SSI的整合和Spring跟XFIRE整合之后,接下来带来是的上传下载:下面直接加入上传下载功能。
项目目录:
下面是 源码及其相关注释:
BaseAction.java (别的Action继承此类可以写多任务处理了。)
package com.unite.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request = arg0;
this.session = this.request.getSession();
}
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
this.response = arg0;
}
}
uploadAction.java
package com.unite.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.unite.util.BaseAction;
public class uploadAction extends BaseAction{
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE =16*1024;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
//下载文件原始存放路径
private final static String DOWNLOADFILEPATH="/UploadImages/";
//文件名参数变量
private String downfileName;
private String downloadChineseFileName;
private InputStream downloadFile;
public String execute() throws Exception {
imageFileName = new Date().getTime()+getExtention(fileName);
File uploadFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages"));
if(!uploadFile.exists())
uploadFile.mkdir();
File imageFile = new File(uploadFile,imageFileName);
copy(myFile, imageFile);
return super.execute();
}
public static void copy(File src,File dst){
try{
InputStream inputStream = null;
OutputStream outputStream = null;
try{
inputStream = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
outputStream = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
byte[] bs = new byte[BUFFER_SIZE];
while(inputStream.read(bs)>0){
outputStream.write(bs);
}
}finally{
if(inputStream!=null){
inputStream.close();
}
if(outputStream!=null){
outputStream.close();
}
}
}catch(Exception exception) {
exception.printStackTrace();
}
}
//下载
public String download(){
downfileName = request.getParameter("downfilename");
System.out.println(downfileName);
//从下载文件原始存放路径读取得到文件输出流
downloadFile = ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+downfileName);
//如果下载文件名为中文,进行字符编码转换
try {
downloadChineseFileName = new String(downfileName.getBytes(), "ISO8859-1");