struts2实现文件上传和下载

文件上传内容:微笑

在jsp页面form表格如下:

<form action="one" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="ti jiao"/>
    </form>

在Action中代码如下:

package cn.itcast.up;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import cn.itcast.formdata.Formdata;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class OneAction extends ActionSupport implements ModelDriven<Formdata>{
	
	private Formdata formdata = new Formdata();

	public Formdata getModel() {
		return formdata;
	}
	
	public String execute() throws Exception{
		System.out.println("文件类型:"+formdata.getFileFileContenType());
		
		String str = ServletActionContext.getServletContext().getRealPath("/up");
		
		str = str + "/" + formdata.getFileFileName();

		System.out.println("............"+formdata.getFile());
	
		InputStream in = new FileInputStream(formdata.getFile());
		OutputStream out = new FileOutputStream(str);
		byte[] b = new byte[1024];
		int len = 0;
		while((len=in.read(b))!=-1){
			out.write(b,0,len);
		}
		out.close();
		in.close();

		return "success";
	}
	

}


 

package cn.itcast.formdata;

import java.io.File;

public class Formdata {
	
	private File file;
	private String fileFileName;
	private String fileFileContenType;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileFileContenType() {
		return fileFileContenType;
	}
	public void setFileFileContenType(String fileFileContenType) {
		this.fileFileContenType = fileFileContenType;
	}

	
}


在struts.xml文件中配置:

<struts>
 <constant name="struts.action.extension" value="action,do,"/>
 <constant name="struts.i18n.encoding" value="UTF-8"></constant>
 <package name="itcast" extends="struts-default">
     <action name="one" class="cn.itcast.up.OneAction">
        <result name="success">/index.jsp</result>
     </action>       
 </package>
</struts>

 

文件下载代码如下:

package cn.itcast.formdata;

import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class DownAction extends ActionSupport{

	private String fileName;

	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public InputStream getFile(){
		System.err.println("得到 文件对像....");
		InputStream in = 
			ServletActionContext.getServletContext()
			.getResourceAsStream("/up/a.jpg");
		return in;
	}
	public String execute() throws Exception {
		String ss = "你好.jpg";
		ss = URLEncoder.encode(ss,"UTF-8");
		setFileName(ss);
		System.err.println("execute....");
		return SUCCESS;
	}
}

struts.xml文件配置如下:

<action name="down" class="cn.itcast.down.DownAction">
			<result type="stream">
				<param name="contentType">image/jpeg</param>
                   <!-- inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数 -->
                                     <param name="inputName">inputStream</param>
			
	                            <param name="contentDisposition">attachment;filename=${fileName}</param>
				<param name="bufferSize">4096</param>
				<!-- 从哪一个方法中获取文件对像 ,根据指明的字符串,组成getXxx-->
				<param name="inputName">file</param>
			</result>
                           <result name="input">/WEB-INF/pages/inputError.jsp</result>

</action>




 

 参考API SteamResult(http://struts.apache.org/2.0.14/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html),其中

contentType是内容类型,根据不同文件类型参照MIME标准设置,本例是文本。

contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment),默认是inline, 使用附件时这样配置:attachment;filename="文件名" 。



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值