利用Struts2完成文件的上传和下载功能

今日,学习了struts2的文件的上传和下载。以下是具体的步骤和代码(此示例的下载是下载上一次上传的文件):
第一步:jsp页面,index.jsp:
<s:form action="upload" method="post" theme="simple" enctype="multipart/form-data">
  <table align="center" width="50%" border="1">
  <tr>
          <td>选择上传文件</td>
          <td id="more">
              <s:file name="upload"></s:file>
             
          </td>
  </tr>
<tr>
         <td><s:submit type="button" value="提交" onclick="return checkf()"/></td>
         <td><s:reset value="重置 "></s:reset></td>
     </tr>
</table>
<table align="center" width="50%" border="1">
<tr>
<td>下载</td>
<td>

      <a href="<s:url value='download.action'></s:url>">下载</a>
</td>
</tr>
</table>
</s:form>

第二部:写action代码:
package com.hp.lwq.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UpDownloadAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
//上传是需要的
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;

//下载时需要的属性
private String downloadFileName;;
private InputStream content;
private String path;




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 getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getDownloadFileName() {
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getContent() throws Exception{
//从session中获取上次上传的文件
String downName=(String) ActionContext.getContext().getSession().get("uploadFileName");
//获取文件的路径
String path = ServletActionContext.getServletContext().getRealPath(
"/uploads/"+downName);
//根据路径找到文件
File file = new File(path);

this.downloadFileName = file.getName();
this.downloadFileName = new String(downloadFileName.getBytes(),"ISO8859-1");
content = new FileInputStream(file);

return content;
}

public void setContent(InputStream content) {
this.content = content;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//================================================================
//上传
public String upload() throws Exception{
//获取Servlet上下文所得到的服务器端详细路径
String realPath = ServletActionContext.getServletContext().getRealPath(
savePath);
/*//將存放路徑保存在request中
ActionContext.getContext().put("realPath", realPath);*/
//根据获得的路径创建目录
File path = new File(realPath);
if (!path.exists()) {
path.mkdirs();
}
//利用输入输出流读写文件
//利用输入流根据action的file属性读取文件
FileInputStream is = new FileInputStream(upload);
//根据action的文件名属性及服务器路径创建输出流
FileOutputStream os = new FileOutputStream(realPath + "/"
+ uploadFileName);
//把刚上传的文件按名字放入session中
ActionContext.getContext().getSession().put("uploadFileName", uploadFileName);
//创建字节数组,接收流并写入文件(此处8k)
byte buffer[] = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
//关闭流
os.close();
is.close();
return SUCCESS;

}

//下载
public String download() {
return SUCCESS;
}




}

第三部:配置struts.xml文件:
<action name="upload" class="com.hp.lwq.action.UpDownloadAction" method="upload">
     <param name="savePath">/uploads</param>
     <result name="success">/result.jsp</result>
     <result name="input">/upload.jsp</result>
  </action>
 
 
  <action name="download" class="com.hp.lwq.action.UpDownloadAction" method="download">
          <param name="path">/downloads</param>
            <result name="success" type="stream">
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                <param name="bufferSize">8192</param>               
                <param name="inputName">content</param>
                <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
            </result>
            <result name="error" >/error.jsp</result>
        </action>
注:上传的目录为tomcat服务器下webapps/uploads文件夹中。
    下载的目录即为用浏览器下载东西所存的目录。

猜你喜欢

转载自1960370817.iteye.com/blog/2252717
今日推荐