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");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return SUCCESS;
}
//获得文件后缀
public static String getExtention(String fileName){
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public void setMyFileContentType(String contentType) {
this.setContentType(contentType);
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public String getDownfileName() {
return downfileName;
}
public void setDownfileName(String downfileName) {
this.downfileName = downfileName;
}
public String getDownloadChineseFileName() {
return downloadChineseFileName;
}
public void setDownloadChineseFileName(String downloadChineseFileName) {
this.downloadChineseFileName = downloadChineseFileName;
}
public InputStream getDownloadFile() {
return downloadFile;
}
public void setDownloadFile(InputStream downloadFile) {
this.downloadFile = downloadFile;
}
}
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri = "/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action ="fileUpload.do" method ="POST" enctype ="multipart/form-data" >
<s:file name ="myFile" label ="Image File" />
<s:textfield name ="caption" label ="Caption" />
<s:submit />
</s:form >
</body>
</html>
uploadSucc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri = "/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<SCRIPT type="text/javascript">
function down(){
var downfilename= document.getElementByIdx_xx_x("downfilename").value;
var url=encodeURI("download.do?downfilename="+downfilename);
document.getElementByIdx_xx_x("aa").innerHTML ="<a href='"+url+"' rel='#overlay' style='text-decoration:none'>下载</a>";
}
</SCRIPT>
</head>
<body οnlοad="down()">
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ='UploadImages/<s:property value ="imageFileName" />' />
<br />
<s:property value ="imageFileName" />
<br/>
<input type="text" id="downfilename" value="<s:property value ='imageFileName' />">
<div id="aa"></div>
</div >
</body>
</html>
struct.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="User_LogAction" class="com.unite.action.User_LogAction">
<result name = "ok">/upload.jsp</result>
<result name = "no">/index.jsp</result>
<result name = "login">/index.jsp</result>
</action>
<!-- 上传下载action配置 -->
<action name="fileUpload" class="com.unite.action.uploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result>/uploadSucc.jsp</result>
</action>
<!-- down -->
<action name="download" class="com.unite.action.uploadAction" method="download">
<result name="success" type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">
image/bmp,image/png,image/gif,image/pjpeg
</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">
attachment;filename="${downloadChineseFileName}"
</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">downloadFile</param>
</result>
</action>
</package>
</struts>
别忘了在根目录下 建一个文件夹UploadImages,放上传的图片。
运行下 可以了。呵呵