有人可以帮我提出一个可以上传这种变体文件的Java Servlet,或者让我知道如何:
5个文件输入类型字段,但只填写其中的1个或2个(在任何文件字段中),并上传那些没有。文件。
我发现了问题:
1)如果我填写的文件字段不是第一个,那么它就不会上传我的文件。 例如我填写第3个文件字段,然后单击上传。该文件将不会上传。但如果我填写第一个,它将被上传。如果我填写任何2-3个字段,但如果我没有按顺序填写它们(1,2,3 ......),它们将不会被上传。
2)我在文件字段下面有单选按钮。如果我没有填写所有文件字段,servlet将不会读取单选按钮值。有人可以告诉我为什么会这样吗?
总之,当且仅当我填写所有5个文件字段并勾选其中一个单选按钮时,它才有效。
HTML代码:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>Energy Efficient Color Mapping Texture Transformer</h3>
Select 5 images to upload: <br />
<form action="UploadServlet" method="post"
enctype="multipart/form-data">
<b>Image 1</b>
<input type="file" name="file1" size="50" />
<br />
<b>Image 2</b>
<input type="file" name="file2" size="50" />
<br />
<b>Image 3</b>
<input type="file" name="file3" size="50" />
<br />
<b>Image 4</b>
<input type="file" name="file4" size="50" />
<br />
<b>Image 5</b>
<input type="file" name="file5" size="50" />
<br /><br />
Select conversion scheme below: <br />
<input type="radio" name="scheme" value="1" /> Sat 25 Hue 9 <br />
<input type="radio" name="scheme" value="2" /> Sat 25 Hue 24 <br />
<input type="radio" name="scheme" value="3" /> Sat 25 Hue 36 <br />
<input type="radio" name="scheme" value="4" /> Sat 50 Hue 9 <br />
<input type="radio" name="scheme" value="5" /> Sat 70 Hue 9 <br />
<input type="radio" name="scheme" value="6" /> Sat 50 Hue 24 <br />
<input type="radio" name="scheme" value="7" /> Sat 70 Hue 36
<br /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
所以说,我想填写图片3和4,并上传这2张图片。 servlet:
// Import required java libraries
import java.io.*;
import java.util.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.output.*;
public class UploadServlet extends HttpServlet {
private boolean isMultipart;
private String filePath;
private int maxFileSize = 5*1024*1024;
private int maxMemSize = 4 * 1024*1024;
private File file ;
public void init( ){
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
// Check that we have a file upload request
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
// Get the file location where it would be stored.
filePath =
getServletContext().getInitParameter("file-upload");
//Create calendar for folder naming purpose
GregorianCalendar gcalendar = new GregorianCalendar();
String date=String.valueOf(gcalendar.get(Calendar.DATE));
String month=String.valueOf(gcalendar.get(Calendar.MONTH)+1);
String year=String.valueOf(gcalendar.get(Calendar.YEAR));
String hour=String.valueOf(gcalendar.get(Calendar.HOUR));
String minute=String.valueOf(gcalendar.get(Calendar.MINUTE));
String second=String.valueOf(gcalendar.get(Calendar.SECOND));
//create a new filepath
filePath= filePath + date+"-"+month+"-"+year+"_"+hour+minute+second;
boolean newfolder= (new File(filePath)).mkdirs();
if (newfolder){
System.out.println("Created new folder");
}
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( fi.isFormField() )
{
out.println("is a form field <br>");
String fieldName = fi.getFieldName();
String fieldValue = fi.getString();
out.println(fieldName+": "+fieldValue);
}
else
//if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath + "\\" +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath + "\\" +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
throw new ServletException("GET method used with " +
getClass( ).getName( )+": POST method required.");
}
}
到目前为止,当我使用java servlet搜索多个文件上传时,我没有看到任何不同的变体。
答案 0 :(得分:0)
好的,所以当任何文件字段为空时会出现此问题。这意味着fileName也是一个空字符串。解决此问题的一种方法是添加条件以跳过空文件:
if( fileName.equals("")){
continue;
}