2014年5月14日星期三

upload.parseRequest (request) returns empty


<html>
<head>
<title>excel导入到数据库</title>
</head>
<body>
<h3>excel导入到数据库:</h3>
选择一个文件进行上传:
<br />
<form action="UploadFile_view.jsp?action=submit" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" /> <br /> <input
type="submit" value="上传" />
</form>
<%
if (request.getParameter("action") != null) {
if (request.getParameter("action").equals("submit")) {
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("c:\\temp"));
ServletFileUpload upload = new ServletFileUpload(
factory);
upload.setSizeMax(maxFileSize);
List fileItems = upload.parseRequest(request);
}
}
}
%>
</body>
</html>

is here fileItems is empty , and there is great God help facie right
------ Solution --------------------- -----------------------
<%
if (request.getParameter ("action")! = null) {
if (request.getParameter ("action"). equals ("submit")) ; {
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext ();
String filePath = context.getInitParameter ("file -upload ");
String contentType = request.getContentType ();
if ((contentType.indexOf ("multipart/form- data ")> = 0)) {
DiskFileItemFactory factory = ; new DiskFileItemFactory ();
factory.setSizeThreshold (maxMemSize) ;
factory.setRepository (new File ("c: \ \ temp"));
ServletFileUpload upload = ; new ServletFileUpload (
factory);
upload.setSizeMax (maxFileSize) ;
List fileItems = ; upload.parseRequest (request);
}
}
}
%>

This one should be written in the action corresponding jsp page in
------ For reference only ----------------------- ----------------
me directly to the background processing and user test jsp pages together , the sentence below the process is returned to the empty phrase , that is, request did not pass over, would like to ask a moderator greatly under what circumstances request transfer less action inside , ah, I'm not using the struts open platform for the company to buy .
------ For reference only -------------------------------------- -
? action = submit "method =" post " marked in red is actually a way to get this way , but your method definition is post.
------ For reference only -------------------------------------- -
Well, I changed the ingredients in the two files inside, as follows
<html>
<head>
<title>excel导入到数据库</title>
</head>
<body>
<h3>excel导入到数据库:</h3>
选择一个文件进行上传:
<br />
<form action="UploadFile.jsp" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" /> <br /> <input
type="submit" value="上传" />
</form>
</body>
</html>
<%
//UploadFile.jsp
File file;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");

// 验证上传内容了类型
String contentType = request.getContentType();

if ((contentType.indexOf("multipart/form-data") >= 0)) {

DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存中存储文件的最大值

factory.setSizeThreshold(maxMemSize);
// 本地存储的数据大于 maxMemSize.
factory.setRepository(new File("c:\\temp"));

// 创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大上传的文件大小
upload.setSizeMax(maxFileSize);
try {
// 解析获取的文件
List fileItems = upload.parseRequest(request);
// 处理上传的文件
out.println(fileItems);
but the measured result was empty. Sorry ah , I was a beginner , you may have more problems
------ For reference only ------------------------- --------------

issue is resolved, the platform will request jsp page request filtering , and more eggs pain is no exception interface provides only In other methods , thank you for help.

没有评论:

发表评论