我正在开发一个Web应用程序,我需要在其中提供一个功能,以便用户可以上传文件(图像)并传递将用于确定文件名的其他数据。我们需要通过相同的表单传递所有数据。我们能够发送其他数据,但图像上传功能无法正常工作。如果我们正在评论用于访问附加数据的部分,那么它工作正常。
发送数据和上传图片的表单
<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>
&#13;
文件上传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);
}
}
需要注释掉图片上传功能的部分
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);
}
}
}