java enctype multipart/form-data 在多数据类型表单中的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15758510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of enctype multipart/form-data in multi data type form
提问by Shrikant
I have html page which contains 3 file input and 3 text inputs. If I use enctype = multipart/form-data in jsp page I am not able to get the test input form fields. These values always show null. If I remove enctype from post form in jsp, I can get the text field inputs but in this case I cannot upload files. So my question is is it possible to have multiple input fields with file input and if yes how to get the text input field names??
我有包含 3 个文件输入和 3 个文本输入的 html 页面。如果我在 jsp 页面中使用 enctype = multipart/form-data ,我将无法获得测试输入表单字段。这些值始终显示为空。如果我从 jsp 中的 post 表单中删除 enctype,我可以获得文本字段输入,但在这种情况下我无法上传文件。所以我的问题是文件输入是否可以有多个输入字段,如果是,如何获取文本输入字段名称?
Any help on this is appreciated..
对此的任何帮助表示赞赏..
Below is the html code
下面是html代码
<html>
<body>
<form method="post" action="upload.jsp" enctype="multipart/form-data">
Office Name: <input type="text" name="officeName" /> <br>
Doc. Description : <input type="text" name="docDesc" /> <br>
Document 1 : <input type="file" name="doc1" /> <br>
Document 2 : <input type="file" name="doc2" /> <br>
Document 3 : <input type="file" name="doc3" /> <br>
Remarks : <input type="text" name="remarks" /> <br>
<br>
<input type="submit" value="submit" />
</form>
And i am retrieving text and file inputs as
我正在检索文本和文件输入
strOffficeName=Request.getParameter("officeName");
strDocDescription=Request.getParameter("docDesc");
strDoc1Path=Request.getParameter("doc1");
strDoc2Path=Request.getParameter("doc2");
strDoc3Path=Request.getParameter("doc3");
strRemarks=Request.getParameter("remarks");
回答by mthmulders
I would take a look at Apache Commons FileUpload. It has a User Guidethat explains how to get file uploads from your request.
我会看看Apache Commons FileUpload。它有一个用户指南,解释了如何从您的请求中获取文件上传。
The section "Processing the uploaded items" shows an example how to process both file uploads and text inputs.
“处理上传的项目”部分显示了如何处理文件上传和文本输入的示例。
回答by jsshah
Create a class called fileUploader which returns ServletFileUpload object
创建一个名为 fileUploader 的类,它返回 ServletFileUpload 对象
private FileUploader()
{
}
public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB)
{
if(uploader == null)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
factory.setRepository(new File(tempDir));
uploader = new ServletFileUpload(factory);
uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
}
return uploader;
}
Now you can process a request and read all the data. It handles the files uploaded as well as other form data.
现在您可以处理请求并读取所有数据。它处理上传的文件以及其他表单数据。
protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
throws FileSizeLimitExceededException
{
if(!isMultipartRequest(request))
return null;
ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
MultiPartFormData data = new MultiPartFormData();
try
{
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items)
{
if(item.isFormField())
{
data.getParameters().put(item.getFieldName(), item.getString());
}
else
{
String filename = item.getName();
//Internet explorer and firefox will send the file name differently
//Internet explorer will send the entire path to the file name including
//the backslash characters etc ... we should strip it down
//THIS IS HACKY
if(filename.indexOf("\") != -1)
{
int index = filename.lastIndexOf("\");
filename = filename.substring(index + 1);
}
if(filename == null || filename.equals(""))
{
//do nothing
}
else
{
File uploadFile = new File(uploadDir + File.separator + randomFileName);
item.write(uploadFile);
data.addFile(item.getFieldname(), item.getString());
}
}
}
}
catch(FileSizeLimitExceededException e)
{
throw e;
}
catch(Exception e)
{
e.printStackTrace();
}
return data;
}
After parsing the request I am storing it in some object called MultipartFormData which can be used to get request parameters
解析请求后,我将其存储在某个名为 MultipartFormData 的对象中,该对象可用于获取请求参数
public class MultiPartFormData {
private Hashtable<String, String> parameters;
private Hashtable<String, String> uploadedFiles;
public MultiPartFormData()
{
this.parameters = new Hashtable<String, String>();
this.uploadedFiles = new Hashtable<String, String>();
}
public Hashtable<String, String> getParameters() {
return parameters;
}
public void setParameters(Hashtable<String, String> parameters) {
this.parameters = parameters;
}
public void getParameter(String paramName) {
if(this.parameters.contains(paramName))
return tyhis.parameters.get(paramName);
return null;
}
public void addFile(String key, String filename) {
uploadedFile.put(key, filename);
}
public void getFilename(String key) {
uploadedFile.get(key);
}
}
回答by Terje Rudi
I had a similar problem, and in that case the reason was: The IT department had set some settings on their firewall that stopped the execution of some of my scripts and only for some of the clients. The halt sent the browser to the root web page of my server. They were not able to explain in detail, but after opening up some filters, it all works as intended. WAMP, PHP7.
我遇到了类似的问题,在那种情况下,原因是:IT 部门在他们的防火墙上设置了一些设置,这些设置停止了我的一些脚本的执行,并且只针对一些客户端。暂停将浏览器发送到我服务器的根网页。他们无法详细解释,但打开一些过滤器后,一切都按预期进行。WAMP,PHP7。