struts2文件的上传与下载

upload.jsp的页面:

 <s:form action="uploadAction" method="post" enctype="multipart/form-data">
       <s:textfield name="title" label="上传的文件名"/>
       <s:file name="upload" label="选择文件"/>
       <s:submit value="上传"/>
       </s:form>

uploadAction

       

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UpLoadAction extends ActionSupport {
    private String title;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    
    private String savePath;
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public File getUpload() {
  return upload;
 }
 public void setUpload(File upload) {
  this.upload = upload;
 }
 public String getUploadContentType() {
  return uploadContentType;
 }
 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }
 public String getUploadFileName() {
  return uploadFileName;
 }
 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getSavePath() {
  return ServletActionContext.getServletContext().getRealPath("/WEB-INF/"+ savePath);
 }
 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }
 public String execute() throws Exception {
  /*
   * E:\project\workspace\apache-tomcat-7.0.29\work\Catalina\localhost\struts2upload\
   * 注意这个位置getUpload()打印出来的东西
   */
  System.out.println(getUpload());
     FileInputStream fis = new FileInputStream(getUpload());
     FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+ getUploadFileName());
     int read;
     byte[] b =  new byte[1024];
     while((read=fis.read(b))>0){
      fos.write(b,0,read);
     }
  return SUCCESS ;
 } 
}

<package name="test" extends="struts-default">
         <action name="uploadAction" class="com.huxin.struts2upload.action.UpLoadAction">
             <result>success.jsp</result>
             <param name="savePath">upload</param>
         </action>
</package>

 

下载:

  <a href="downloadAction">下载2图片</a>

 

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport{
	private String inputPath;
	public String getInputPath() {
		return inputPath;
	}
	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}
	/*
	 * InputStream入口
	 */
	public InputStream getTargetFile() throws Exception{
		return ServletActionContext.getServletContext().getResourceAsStream(inputPath);	
	}
	/*
	public String execute() throws Exception {
		return LOGIN;
	}	
	*/
}


 

  <action name="downloadAction" class="com.huxin.struts2upload.action.DownLoadAction">
              <param name="inputPath">/uploadFiles/2.jpg</param>
              <result type="stream">
                  <!-- 指定文件的类型 application/pdf ,application/zip ,images/jpg, -->
                  <param name="contentType">images/jpg</param>
                  <param name="inputName">targetFile</param>
                  <!-- attachment不能掉,是告诉客户端要下载这个2.jpg的文件 -->
                  <param name="contentDisposition">attachment;filename="2.jpg"</param>
                  <param name="buffersize">5000</param>
              </result>
              <result name="login">
                  /WEB-INF/view/login.jsp
              </result>
          </action>    


 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
   <filter>
       <filter-name>struts2-cleanup</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>struts2-cleanup</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>



 

 

          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值