一、文件上传
实现文件上传的方式有很多种,比如Apache的Common-FileUpload,但是实现起来非常的繁琐,而Struts2来实现文件上传则简单的多,默认也是基于Common-FileUpload的方式。
1.使用Struts2来实现文件上传,需要使用Struts2预定义的fileUpload拦截器,defaultStack拦截器栈引用了fileUpload拦截器;最后,struts-default包又将defaultStack拦截器声明为自己的默认拦截器引用。使用defaultStack拦截器栈,也从而引用了fileUpload拦截器
JSP页面
<body> <s:fielderror></s:fielderror> <form action="upload.action" method="post" enctype="multipart/form-data"> <input type="file" name="image"/><br> <input type="file" name="image"/><br> <input type="submit"><br> </form> </body>
文件上传Action
public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private File[] image; private String[] imageFileName; private String[] imageContentType; // 注意必须是xxxFileName,xxxContentType,xxx就是File 对象的引用名 @Override public String execute() throws Exception { String fileLocation = "D:\\mytemp";// 指定一个上传文件的存储位置 for (int i = 0; i < image.length; i++) { // String fileName = // fileLocation+File.separatorChar+imageFileName[i]; File destFile = new File(fileLocation, imageFileName[i]); FileUtils.copyFile(image[i], destFile); System.out.println("上传文件的名:" + imageFileName[i]); System.out.println("上传文件的类型:" + imageContentType[i]); } return SUCCESS; } public File[] getImage() { return image; } public void setImage(File[] image) { this.image = image; } public String[] getImageFileName() { return imageFileName; } public void setImageFileName(String[] imageFileName) { this.imageFileName = imageFileName; } public String[] getImageContentType() { return imageContentType; } public void setImageContentType(String[] imageContentType) { this.imageContentType = imageContentType; } }Struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="fileupload"></constant> <!-- 限制上传文件的总大小 --> <constant name="struts.multipart.maxSize" value="10485760"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class="cn.cil.action.FileUploadAction"> <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedExtensions"> .jpeg,.gif,.bmp,.jpg,.png </param> <param name="fileUpload.maximumSize">2097152</param> </interceptor-ref> <result name="input">/failure.jsp</result> <result>/success.jsp</result> <result name="fail">/failure.jsp</result> </action> </package>
在引用fileUpload拦截器的时候,可以指定三个参数(指定<param>子元素):
应用国际化,自定义错误提示消息
关于name的值可以在struts-core-2.3.15.3.jar中的struts-messages_xx.properties中找到
二、文件下载
使用Struts2来实现文件下载,会用到它的stream类型的Result,这种Result最终会返回一个InputStream,只需要让这个InputStream能读到用户想要下载的文件
struts.xml
<package name="down" extends="struts-default"> <action name="download" class="cn.cil.action.FileDownAction" method="download" > <result name="success" type="stream"> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="contentType">application/octet-stream</param> </result> </action> </package> </struts>
public class FileDownAction extends ActionSupport { private InputStream inputStream;//定义一个输入流。名字不能为in private String fileName; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String download() throws Exception{ //实现下载:给inputStream赋值 String realPath = ServletActionContext.getServletContext().getRealPath("/六6.jpg"); //解决中文乱码 fileName = URLEncoder.encode(FilenameUtils.getName(realPath),"UTF-8"); //System.out.println(fileName); inputStream = new FileInputStream(realPath); return SUCCESS; } public String download1()throws Exception{ String realPath = "D:\\mytemp\\456.jpg"; fileName = URLEncoder.encode(FilenameUtils.getName(realPath), "UTF-8"); inputStream = new FileInputStream(realPath); return SUCCESS; } }