commons-fileupload上传下载
Struts2 commons-fileupload上传下载示例
Action代码:
package com.yoyolg.action;
?
import java.io.File;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.net.URLDecoder;
?
import org.apache.struts2.ServletActionContext;
?
import com.opensymphony.xwork2.ActionSupport; ?
/**
?* 文件上传下载示例Action
?*
?* @author 杨砚
?*/
public class UpAndDownAction extends ActionSupport {
????/**
?????* 序列化编号
?????*/
????private static final long serialVersionUID = 1383989032015709062L;
?
????/**
?????* 存放上传文件的属性(上传页面中需在HTML的file标签中指定name属性为upload) ?????*/
????File upload;
?
????/**
?????* 上传文件名(上传时无需在页面指定,由Struts2自动绑定)
?????*/
????String uploadFileName;
?
????/**
?????* 随上传文件一起提交的
单数据(该字段仅为演示在上传文件时提交的其他表单数据)
?????*/
????String userName;
?
????/**
?????* 下载文件名
?????*/
????String downLoadfileName;
?
????/**
?????* 文件上传方法(Struts2 Action动态方法)
?????*
?????* @return String upLoadSuccess 上传成功后跳转到upLoad.jsp页面
?????* @throws Exception
?????*/
????public String upLoad() throws Exception { ????????// 打印用户名,演示随上传文件一起提交的表单数据
????????System.out.println(this.userName); ????????if (this.upload != null) { ????????????// 文件存放路径为工程运行时根目录下的file文件夹
????????????FileOutputStream fos = new FileOutputStream(ServletActionContext
????????????????????.getServletContext().getRealPath("/")
????????????????????+ "file\\"
????????????????????+ this.uploadFileName); ????????????FileInputStream fis = new FileInputStream(this.upload);
????????????// 以下语句通过输入流读取上传文件内容然后再通过输出流将内容输出到指定文件中
????????????byte[] b = new byte[1024]; ????????????int len = 0;
????????????while ((len = fis.read(b)) > 0) { ????????????????fos.write(b, 0, len); ????????????}
????????}
????????return "upLoadSuccess";
????}
?
????/**
?????* 文件下载Action方法
?????*
?????* @return
?????* @throws Exception
?????*/
????public String downLoad() throws Exception { ????????return "downLoadSuccess";
????}
?
????/**
?????* Struts2自动调用的文件下载方法
?????*
?????* @return
?????*/
????public InputStream getDownloadFile() throws Exception { ????????// 将通过GET方式(超链接)提交的下载文件名解码(解决中文乱码)
????????// 页面上需要使用javaScript的encodeURI方法编码
????????this.downLoadfileName = URLDecoder.decode(this.downLoadfileName,
????????????????"UTF-8");
????????try {
????????????// 获取文件流,这里使用的是解码之后的文件名来获取,
????????????// 获取之后还需对downLoadfileName进行再次编码
????????????FileInputStream fileInputStream = new FileInputStream(
????????????????????ServletActionContext.getServletContext().getRealPath("/")
????????????????????????????+ "file\\" + this.downLoadfileName);
????????????// 再次对文件名进行编码(否则下载文件名将出现乱码或无法下载)
????????????this.downLoadfileName = new String( ????????????????????this.downLoadfileName.getBytes(), "ISO-8859-1");
????????????return fileInputStream;
????????} catch (UnsupportedEncodingException e) { ????????????e.printStackTrace();
????????} catch (FileNotFoundException e) { ????????????e.printStackTrace();
????????}
?
????????return null;
????}
?
????public File getUpload() {
????????return upload;
????}
?
????public void setUpload(File upload) { ????????this.upload = upload;
????}
?
????public String getUploadFileName() {
????????return uploadFileName;
????}
?
????public void setUploadFileName(String uploadFileName) { ????????this.uploadFileName = uploadFileName; ????}
?
????public String getUserName() {
????????return userName;
????}
?
????public void setUserName(String userName) { ????????this.userName = userName;
????}
?
????public String getDownLoadfileName() { ????????return downLoadfileName;
????}
?
????public void setDownLoadfileName(String downLoadfileName) {
????????this.downLoadfileName = downLoadfileName; ????}
?
}
上传页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
????pageEncoding="UTF-8"%>
Insert title here
????
下载页面 ????
注意事项:文件上传后要看上传效果,请查看运行时工程根目录下的file文件夹 ????如果要查看下载效果,请上传一个名为"测试文件.txt"的文件
????