commons-fileUpload.jar fileUpload.parseRequest(request);为什么获取不到值

so_co 发布于 2014/05/16 13:02
阅读 5K+
收藏 0

fileUpload.parseRequest(request);为什么获取不到值?

上传servlet:

   
    //上传路径
    File file = new File("d:/upload");
    //临时文件路径
    File tempFile = new File("d:/uploadtemp");
    if(!file.exists()){
    file.mkdir();
    }
    if(!tempFile.exists()){
    tempFile.mkdir();
    }
    if(ServletFileUpload.isMultipartContent(request)){
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(1024*10);
    factory.setRepository(tempFile);
    ServletFileUpload fileUpload = new ServletFileUpload(factory);
    fileUpload.setSizeMax(1024*1024*10);
    try {
List<FileItem> items =  fileUpload.parseRequest(request);
for (FileItem item : items) {
if(!item.isFormField()){
String name = item.getName();
System.out.println("name:" + name);
System.out.println("length:" + item.getSize());
item.write(new File("d:/upload/"+name));
}else{
String desc = item.getString("UTF-8");
System.out.println("文件描述:"+desc);
}
}
}  catch (Exception e) {
e.printStackTrace();
}
    }else{
    System.out.println("==========================");
    throw new RuntimeException("请设置form表单的enctype属性");
    }

前台表单:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>upload page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  <body>
    <form action="servlet/UpLoadServlet" enctype="multipart/form-data" method="post">
    文 件:<input type="file" name=""/><input type="submit" value="上传"/>
    </form>
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>UpLoadServlet</servlet-name>
    <servlet-class>com.cmbc.servlet.UpLoadServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>UpLoadServlet</servlet-name>
    <url-pattern>/servlet/UpLoadServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

以下是问题补充:

@so_co:List<FileItem> items = fileUpload.parseRequest(request);返回值直接是空的! (2014/05/16 14:40)
加载中
0
CloudArchitect
CloudArchitect
核心就是前台表单的编码 和 诸如struts2之类的拦截器把请求参数意外的过滤了
so_co
so_co
回复 @程序猿可爱多 : 确实如此!!!哥们儿谢谢了
CloudArchitect
CloudArchitect
回复 @so_co : 文 件:<input type="file" name=""/> 最好加上name和id属性并且赋值
CloudArchitect
CloudArchitect
回复 @so_co : 你是用纯serverlet没有用其他的MVC(Struts之类)的框架吧 ? 如果是这样的话还得发下web.xml
so_co
so_co
回复 @程序猿可爱多 : 编码是UTF-8
so_co
so_co
回复 @程序猿可爱多 : 前面表单如上
下一页
0
so_co
so_co
大牛们哪去了...
0
felix_ding
felix_ding
被web.XML文件的过滤器过滤掉了
返回顶部
顶部