I am working on a web application in which I need to provide a functionality so that user can upload a file(image) and should pass additional data which will be used in determining the file name. We need to pass all data through the same form. We are able to send additional data but then image upload functionality is not working. If we are commenting out the part which is used for accessing the additional data then it is working fine.
Form for sending data and uploading image
<form id="UploadFile" action="UploadFile" method="post"
enctype="multipart/form-data">
<input type="file" name="file" /> <input type="hidden" value="upload" />
<input type="text" name="Notes" placeholder="Evidence Notes">
<input type="radio" name="evidenceof" value="Servicepack" checked>Servicepack
<input type="radio" name="evidenceof" value="Windowspatch">
Windowspatch <input type="radio" name="evidenceof" value="AVpatch">
AVpatch <BR><input type="submit" value="Submit">
</form>
The file upload servlet
@WebServlet("/UploadFile")
public class UploadFile extends HttpServlet {
static Connection con=null;
private static final long serialVersionUID = 1L;
private final String UPLOAD_DIRECTORY = "C:/Files/";
public static String FileType(final String fileName)
// method to determine the type of file
{
String fileType = "Undetermined";
final File file = new File(fileName);
try
{
fileType = Files.probeContentType(file.toPath());
}
catch (IOException ioException)
{
System.out.println(
"ERROR: Unable to determine file type for " + fileName
+ " due to exception " + ioException);
}
return fileType;
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String Notes = null;
String evidenceof = null;
con=DBConnector.getConnector(); //to get last audit ID and accordingly create filename for audit evidence
PreparedStatement ps1;
String init=null;
try {
ps1 = con.prepareStatement("select * from tbl_audithost");
ResultSet rs1=ps1.executeQuery();
while(rs1.next())
{
init=rs1.getString("AuditID");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items1 = null;
try {
items1 = upload.parseRequest(request);
} catch (FileUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Iterator iter = items1.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName(); //text
String value = item.getString();
//System.out.println("name: "+ name + " value: "+ value );
if (name.equals("Notes")) {
Notes = value;
System.out.println(value);
}
else if (name.equals("evidenceof")) {
evidenceof = value;
System.out.println(value);
}
}
}
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// process only if its multipart content
if (isMultipart) {
// Create a factory for disk-based file items
FileItemFactory factory1 = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload1 = new ServletFileUpload(factory1);
try {
// Parse the request
List<FileItem> multiparts = upload1.parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
String name = new File(item.getName()).getName();
String filetype = UploadFile.FileType(name);
System.out.println("The file type is:" + filetype);
String newname = init+evidenceof+".jpg";
System.out.println(newname);
item.write(new File(UPLOAD_DIRECTORY + File.separator + newname));
}
}
// File uploaded successfully
request.setAttribute("message", "Your file has been uploaded!");
}
catch (Exception e)
{
request.setAttribute("message", "File Upload Failed due to " + e);
}
} else
{
request.setAttribute("message", "This Servlet only handles file upload request");
}
request.getRequestDispatcher("/SystemAdded.jsp").forward(request, response);
}
}
Part which needs to commented out for proper functionality of Image upload
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items1 = null;
try {
items1 = upload.parseRequest(request);
} catch (FileUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Iterator iter = items1.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName(); //text
String value = item.getString();
//System.out.println("name: "+ name + " value: "+ value );
if (name.equals("Notes")) {
Notes = value;
System.out.println(value);
}
else if (name.equals("evidenceof")) {
evidenceof = value;
System.out.println(value);
}
}
}