I have a JSP page with an HTML form:
我有一个带有HTML表单的JSP页面:
Here is my code:
这是我的代码:
<form method="post" action="UploadServlet" name="upload" enctype="multipart/form-data" >
Select file to upload: <input type="file" name="uploadFile" >
<input type="text" name="fileNames" >
<input type="hidden" name="form_id" value="652973" >
<br/><br/>
<input type="submit" value="Upload" />
</form>
web.xml
web.xml中
<display-name>UploadServletApp</display-name>
<servlet>
<description></description>
<display-name>UploadServlet</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>net.code.upload.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
</web-app>
UploadServlet.java
UploadServlet.java
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(THRESHOLD_SIZE);
String paths="C:\\Uploaded_FIle\\";
factory.setRepository(new File(paths));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
// constructs the directory path to store upload file
//String uploadPath = getServletContext().getRealPath("")+ File.separator + UPLOAD_DIRECTORY;
String uploadPath =paths+File.separator + UPLOAD_DIRECTORY;
// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
try {
// parses the request's content to extract file data
List formItems = upload.parseRequest(request);
Iterator iter = formItems.iterator();
// iterates over form's fields
while ( iter.hasNext () )
{
FileItem fi = (FileItem)iter.next();
if ( !fi.isFormField () ) {
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
File file = new File( uploadDir + "yourFileName") ;
fi.write( file ) ;
}
}
request.setAttribute("message", "Upload has been done successfully!\n" + uploadDir + "<br>");
} catch (Exception ex) {
request.setAttribute("message", "There was an error: " + ex.getMessage());
}
The problem is, I am able to upload the file, but the values which I'm sending are not coming, like fileNames,form_id. I am not getting any values which entered through jsp page as well as hard-coded values.
问题是,我能够上传文件,但我发送的值不会来,如fileNames,form_id。我没有获得通过jsp页面输入的任何值以及硬编码值。
1 个解决方案
#1
0
This snippet if ( !fi.isFormField () )
in your code skips the fields which you are sending and only processes the files that are sent. So to get the fields you need to handle both the cases:
代码中的这个代码段(!fi.isFormField())会跳过您要发送的字段,并且只处理发送的文件。因此,要获取需要处理这两种情况的字段:
- When the field is a form field like radio-button, text, etc.
- 当字段是一个表单字段,如单选按钮,文本等。
- When the field is a file.
- 当字段是文件时。
This should give you an idea.
这应该给你一个想法。
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldName = item.getFieldName();
String fieldValue = item.getString();
// ... (do your job here)
} else {
// Process form file field (input type="file").
String fieldName = item.getFieldName();
String fileName = FilenameUtils.getName(item.getName());
InputStream fileContent = item.getInputStream();
// ... (do your job here)
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
#1
0
This snippet if ( !fi.isFormField () )
in your code skips the fields which you are sending and only processes the files that are sent. So to get the fields you need to handle both the cases:
代码中的这个代码段(!fi.isFormField())会跳过您要发送的字段,并且只处理发送的文件。因此,要获取需要处理这两种情况的字段:
- When the field is a form field like radio-button, text, etc.
- 当字段是一个表单字段,如单选按钮,文本等。
- When the field is a file.
- 当字段是文件时。
This should give you an idea.
这应该给你一个想法。
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldName = item.getFieldName();
String fieldValue = item.getString();
// ... (do your job here)
} else {
// Process form file field (input type="file").
String fieldName = item.getFieldName();
String fileName = FilenameUtils.getName(item.getName());
InputStream fileContent = item.getInputStream();
// ... (do your job here)
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}