struts upload download

1.action:
public String fileUpload()throws Exception{
FileOutputStream out=null;
//String cDate = DateUtils.convertDateString(new Date(), "yyyyMMdd");
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/");
String filename = path+File.separator+uploadFileName;
System.out.println(upload.getAbsolutePath()+" "+upload.getName());
String filename2 = path+File.separator;
FileInputStream in = new FileInputStream(upload);
File f=new File(filename2);
if(!f.exists()){
f.mkdirs();
out= new FileOutputStream(new File(filename));
}
else{
out= new FileOutputStream(new File(filename));
}
byte[]b = new byte[1024];
int len = 0;
while((len=in.read(b))>0){
out.write(b,0,len);
}
out.close();//关闭流;
in.close();
return SUCCESS;
}


public void print_file(String dir) {
File file = new File(dir);
File files[] = file.listFiles();
for (File tempFile : files) {
if (tempFile.isDirectory()) {

print_file(tempFile.getAbsolutePath());
} else {
fileCount++;
filenameList.add(tempFile.getAbsolutePath().substring(tempFile.getAbsolutePath().lastIndexOf("\\")+1,
tempFile.getAbsolutePath().length()));
}
}

}


public String downloadFile(){
inputStream = ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/upload/" + fileName);

return SUCCESS;

}

2
<actionname="downloadFile"class="XXX.XXX" method="downloadFile">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param><!-- inline 为打开方式 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">2048</param>
</result>
</action>

contentType
内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片

inputName
下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法

contentDisposition
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:

attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"


3
<%@ 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>
<title>My JSP '1.jsp' starting page</title>
</head>

<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="upload" label="上传"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>

4
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h3>下载文件</h3>
<table align="center" border="0">
<tr>
<td width="10%">序号</td>
<td width="20%">文件类型</td>
<td width="60%">文件名</td>
<td width="10%">下载</td>
</tr>
<tr>
<s:iterator status="status" value="filenameList" var="f">
<tr>
<td><s:property value="#status.index+1" /></td>

<td><img src="${pageContext.request.contextPath}/WEB-INF/upload/<s:property value="#f"/>" width="47" height="19" border="0" ></img></td>

<td><s:property value="#f" /></td>

<td><a href="${pageContext.request.contextPath}/downloadFile.action?fileName=<s:property value="#f" />">download</a></td>
</tr>
</s:iterator>
</table>
<hr>
<s:debug></s:debug>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值