我有html页面,其中包含3个文件输入和3个文本输入。如果我在jsp页面中使用enctype = multipart / form-data,我将无法获得测试输入表单字段。这些值始终显示为null。如果我从jsp中的post表单中删除enctype,我可以获得文本字段输入,但在这种情况下我无法上传文件。所以我的问题是有多个输入字段可以输入文件,如果是,如何获取文本输入字段名称?
对此有任何帮助表示赞赏..
以下是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>
我正在检索文本和文件输入
strOffficeName=Request.getParameter("officeName");
strDocDescription=Request.getParameter("docDesc");
strDoc1Path=Request.getParameter("doc1");
strDoc2Path=Request.getParameter("doc2");
strDoc3Path=Request.getParameter("doc3");
strRemarks=Request.getParameter("remarks");
答案 0 :(得分:1)
我会看看Apache Commons FileUpload。它有一个User Guide,解释了如何从您的请求中上传文件。
“Processing the uploaded items”部分显示了如何处理文件上传和文本输入的示例。
答案 1 :(得分:0)
创建一个名为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;
}
现在您可以处理请求并读取所有数据。它处理上传的文件以及其他表单数据。
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;
}
解析请求后,我将其存储在一个名为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);
}
}
答案 2 :(得分:0)
我遇到了类似的问题,在这种情况下,原因是:IT部门在其防火墙上设置了一些设置,这些设置停止了我的某些脚本的执行,并且仅针对某些客户端。暂停将浏览器发送到我服务器的根网页。他们无法详细解释,但是在打开一些过滤器后,所有功能均按预期工作。 WAMP,PHP7。