一实现上传
1.拷贝common提供的fileupload组件(2个jar)到/WEB-INF/lib目录中。
a.commons-fileupload-1.2.jar b。commons-io-1.3.1.jar
2 在Action中写:
private File upload;
private String uploadFileName
public String upload() throws Exception
{
HttpServletRequest request=ServletActionContext.getRequest();
String targetFile = request.getRealPath("/upload")+"/"+this.uploadFileName;
FileInputStream in=new FileInputStream(upload);
FileOutputStream out = new FileOutputStream(targetFile);
byte[] buffer=new byte[100*1024];
int length=-1;
while((length=in.read(buffer))>0)
{
out.write(buffer, 0, length);
}
out.close();
in.close();
return "success";
}
3.在strutx.xml中声明上传组件的类型.
<constant name="struts.multipart.parser" value="jakarta"></constant>
//设定上传文件大小的上限,默认为2M
<constant name="struts.multipart.maxSize" value="209715200"></constant>
//设定请求的编码,处理中文文件名
<constant name="struts.i18n.encoding" value="gbk"></constant>
二:整合spring
1 给项目添加spring
2 将整合spring的插件struts-spring-plugin.jar到/WEB-INF/lib目录下。
3 在web.xml中配置
<context-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB- INF/classes/applicationContext.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>