struts2中文件的上传与下载

upload.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/Action4_add" method="post"   enctype="multipart/form-data">
<input type="file" name="ff">
<input type="submit" name="提交">
</form>
</body>
</html>

Action.java
public class Action4 extends ActionSupport{
//文件下载
private String nn;
//上传文件
private File ff;
//上传文件名
private String ffFileName;
//上传文件类型
private String ffContentType;

public String add() throws IOException {
	//System.out.println("sdf");
	
	FileUtils.copyFile(ff, new File("f:"+File.separator+"a.jpg"));
	return "add";
}

public String dd() {
	
	return "dd";
}

public String getContextType() {
	return ServletActionContext.getServletContext().getMimeType(nn);
}

public String getDownloadFileName() throws UnsupportedEncodingException{
	return new String(nn.getBytes("utf-8"),"iso8859-1");
}

public InputStream getInputStream() throws IOException {
	nn=new String(nn.getBytes("iso8859-1"),"utf-8");
	FileInputStream fis=new FileInputStream(new File("d:"+File.separator+nn));
	return fis;
}
//*************
public File getFf() {
	return ff;
}
public void setFf(File ff) {
	this.ff = ff;
}
public String getFfFileName() {
	return ffFileName;
}
public void setFfFileName(String ffFileName) {
	this.ffFileName = ffFileName;
}
public String getFfContentType() {
	return ffContentType;
}
public void setFfContentType(String ffContentType) {
	this.ffContentType = ffContentType;
}

public String getNn() {
	return nn;
}

public void setNn(String nn) {
	this.nn = nn;
}
}

struts.xml

<package name="default" namespace="/" extends="struts-default">	
	<interceptors>
	     <interceptor name="interceptor1" class="com.baidu.interceptor.Interceptor1"> </interceptor>
	</interceptors>
	<action name="Action4_*" class="com.baidu.action.Action4" method="{1}">
	     <result name="add">/success.jsp</result>
	     <result name="dd" type="stream">
	            <param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
				<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
				<param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
	     </result>
	     <!-- 上传时只限制action文件的大小 -->
	     <interceptor-ref name="defaultStack">
	         <param name="fileUpload.maximumSize">20</param>
	     </interceptor-ref>
	</action>
</package>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值