使用smartupload上传过大文件,或者多文件的时候可能出现CPU或内存占用过高的问题.并且:只有重新启动容器才能恢复正常!后决定用用commons-fileupload.
下载:下载地址:http://jakarta.apache.org/site/downloads/downloads_commons-fileupload.cgi,包内包含了API文档.(还有commons-beanutils) 在项目中添加下载的组件。
注意事项:
1.不要在jsp页面中使用struts得标签,使用后,通过正常方式无法接收到相关参数。(不用<html:form>)
2.如果使用action,不要使action继承其他得action,仅仅继承org.apache.struts.action.Action;并且在配置文件中,不要给action配置actionForm,否则通过一下方法:
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(41943040);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(2048);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
接收到得list是null。不关联actionform即可正常使用。
例子:
一。创建defalut.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<body>
This a struts page.
<br>
<form action="/abc/default.do" method="post" name="uploadform"
enctype="multipart/form-data">
<br>
文件1:
<input name="file1" size="40" type="file">
<br>
文件2:
<input name="file2" size="40" type="file">
<br>
<input name="upload" type="submit" value="开始上传" />
</form>
</body>
</html:html>
二。创建FormfileAction 继承Action
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class FormfileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(41943040);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4000);
// 设置临时目录:
fu.setRepositoryPath("c://temp//oo");
// 得到所有的文件:
List fileItems;
try {
fileItems = fu.parseRequest(request);
Iterator iter = fileItems.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
System.out.println("file name:" +name);
System.out.println("file size:" +size);
name = name.replace(":","_");
name = name.replace("//","_");
try {
item.write(new File("F://"+ name));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("execute");
return null;
}
}
三。配置struts-config.xml:
<action
path="/default"
scope="request"
type="com.yourcompany.struts.action.FormfileAction" />