多数据类型表格中的数据
我有html页面,其中包含3个文件输入和3个文本输入。 如果我在jsp页面中使用enctype = multipart / form-data,我无法获得测试输入表单字段。 这些值始终显示为空。 如果我从jsp的帖子表单中删除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");
我会看看Apache Commons FileUpload。 它有一个用户指南,说明如何从您的请求中获取文件上传。
“处理上传的项目”部分显示了如何处理文件上传和文本输入的示例。
创建一个名为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);
}
}
链接地址: http://www.djcxy.com/p/64187.html