Struts2之文件上传

具体步骤:1、在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar(在struts2.0版本已集成commons-fileupload,在struts2.1后被省去,得导入)2、将form表的enctype设置为:"multipart/form-data",如下:<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post"> <input type="file" name="uploadImage"></form>3、在action类中添加以下属性,属性红色加粗部分对应于表单中文件字段的名称:public class HelloWorldAction{ private File uploadImage;//得到上传的文件 private String uploadImageContentType;//得到文件的类型 private String uploadImageFileName;//得到文件的名称 /***属性的setter和getter方法***/ public String upload() throws Exception{ String realpath=ServletActionContext.getServletContext().getRealPath("/images"); File file=new File(realpath); if(!file.exists()) file.mkdirs(); FileUtil.copyFile(uploadImage,new File(file,uploadImageFileName)); return "success"; }}

完整代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'fileUpload.jsp' starting page</title></head><body><formaction="${pageContext.request.contextPath}/control/list_execute.action"enctype="multipart/form-data" method="post">文件:<input type="file" name="uploadfile"> <input type="submit"value="上传" /></form></body></html>package struts2.example.action;import java.io.File;import java.io.IOException;import java.util.Arrays;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private File uploadfile;private String uploadfileFileName;// 上传文件的文件名称public File getUploadfile() {return uploadfile;}public void setUploadfile(File uploadfile) {this.uploadfile = uploadfile;}public String getUploadfileFileName() {return uploadfileFileName;}public void setUploadfileFileName(String uploadfileFileName) {this.uploadfileFileName = uploadfileFileName;}public String execute() {// 得到站点下files文件夹的绝对路径String realpath = ServletActionContext.getServletContext().getRealPath("/files");System.out.print(realpath);if (uploadfile != null) {/** 参数1:文件保存的目录 参数2:文件保存的文件名*/File saveFile = new File(new File(realpath), uploadfileFileName);// 用saveFile.getParentFile()得到目录,如果目录不存在,,则创建该目录if (!saveFile.getParentFile().exists())saveFile.getParentFile().mkdirs();// 对文件进行保存try {FileUtils.copyFile(uploadfile, saveFile);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}ActionContext.getContext().put("upload_success", "文件上传成功!");}return "success";}}<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"""><struts><constant name="struts.multipart.maxSize" value="10701096" /><package name="csdn" namespace="/control" extends="struts-default"><action name="list_*" class="struts2.example.action.HelloWorldAction"method="{1}"><!– 定义处理结果与视图资源之间的关系 –><result name="success">/WEB-INF/page/showInfo.jsp</result></action></package></struts>

由于struts2本身上传文件大小的限制,如果上传的文件大小大于限制的值,会出现bug,可通过在struts.xml设置常量可改变上传文件限制的大小

<constant name="struts.multipart.maxSize" value="10701096">

如果上传的文件过大,如上百兆,应通过应用软件上传,通过web上传不稳定。

如果要同时上传多个文件,做几处修改即可。要注意客户端上传文件字段要相同,将action里的文件属性设为数组。

『 不可能 』只存在於蠢人的字典里

Struts2之文件上传

相关文章:

你感兴趣的文章:

标签云: