1:struts.xml
<action name="fileUtilAction" class="com.asia.home.cn.web.announce.FileUtilAction">
<result name="input">/main/announce2.0/error.jsp</result>
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
<param name="savePath">/annnounceUpload</param>
<result type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
//fileName 下载时弹出窗显示的文件名。与action中的fileName属性对应
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">20096</param>
</result>
</action>
2:action
package com.asia.home.cn.web.announce;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.asia.home.cn.base.AsiaBaseAction;
import com.asia.home.cn.utils.UrlUtils;
public class FileUtilAction extends AsiaBaseAction implements SessionAware,
ServletRequestAware, ServletResponseAware {
private static final long serialVersionUID = 1L;
private String fileName; //下载用
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return request.getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadContentType() {
return (this.uploadContentType);
}
public String[] getUploadFileName() {
return (this.uploadFileName);
}
//上传
public void uploadFile()
{
File[] files = getUpload();
FileInputStream fis = null;
FileOutputStream fos =null;
PrintWriter out = null;
try{
for (int i = 0 ; i < files.length ; i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
out = response.getWriter();
File file = new File(getSavePath());
if(!file.exists()){
file.mkdir();
}
fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()[i]);
fis = new FileInputStream(files[i]);
byte[] buffer = new byte[10024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
}
out.write("true");
}
catch(Exception e){
out.write("false");
e.printStackTrace();
}
finally{
try {
fos.close();
fis.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream getInputStream() {
try {
//如果没有以下这句话,下载窗口中的中文名就乱码了。。。。
response.setHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(fileName,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ServletActionContext.getServletContext().getResourceAsStream("/annnounceUpload/" + fileName);
}
//下载
public String downLoad(){
return "success";
}
public void setFileName(String fileName) {
this.fileName = UrlUtils.decode(UrlUtils.decode(fileName,"utf-8"),"utf-8");
}
public String getFileName() {
return fileName;
}
}
3:html
上传:
<form action="fileUtilAction!uploadFile.action" method="post" id="form1" name="form1" enctype="multipart/form-data">
<input type='hidden' name="<%=TokenUtils.TOKEN_STRING_NAME %>" value="<%=sessionToken%>"/>
<table id="myTable" width="100%" cellpadding="3" border=1 style="border:1px solid #cccccc;line-height:30px;font-size:13px;BACKGROUND-COLOR: #fefeed;BORDER-COLLAPSE: collapse;">
<tr align="center">
<td colspan="4">文件1: <input id="sd" type="checkbox" /><input type="file" style="width:200px;" id="fileName" name="upload" /></td>
</tr>
</table>
<table>
<tr>
<td align="center">
<input type='button' style='border:1px solid #000;cursor:pointer; background:#ff6600; color:#fff;' onclick="submitForm();" value=' 保 存 '/>
<input type='button' style="border:1px solid #000;cursor:pointer; background:#ff6600; color:#fff" value=' 取 消 ' onclick="javascript:window.close();" style='cursor: hand;' />
<input type="button" value="添加一行" id="newBtn"/>
<input type="button" value="删除一行" id="delBtn"/>
</td>
</tr>
</table>
</form>
下载:
function download(){
var auditStatus = encodeURI(encodeURI("SQL高级.doc"));
window.location.href="fileUtilAction!downLoad.action?fileName="+auditStatus;
}
分享到:
相关推荐
struts2实现文件上传。解决中文url和中文文件名乱码问题
Struts2文件上传下载 上传为单个上传, 下载包括中文乱码的问题:前台传后台时中文乱码,下载时显示时中文乱码,以及URLEncoder
很受网络上庞杂的Struts2上传下载的误导,很多没有解决中文乱码问题,本例实现了Struts2批量上传与下载,中文乱码问题完美解决,供大家参考!
Myeclipse 6.5, JDK1.6,Tomcat 6.0 未解决点击取消报错的问题,可能是jdk版本问题
之前,为中文文件名出现乱码在google上搜索是大量资源,说的很好,但都未能通过验证。此次认真阅读,上机调试终于通过。
实现了struts框架中文件的下载,下载操作解决了中文乱码问题,同时还包括了任意数目的文件上传操作,压缩文件中有整个源代码,还包括有工程的使用说明。
今天主要分享开源框架Struts2文件上传实例过程,并且笔者将带着大家解决出现的一系列乱码问题,本文章中的重要部分将用特殊颜色标识,斜体表示不确定内容。笔者建议读者先快速阅读一遍本文,下载应具备的工具,再...
使用struts2.0上传文件,完全解决了中文乱码问题。运行项目前请先在D盘建立个名tmp的文件夹,实例上传的文件将保存在tmp文件夹中。文件大小限制为15M,该示例蛮完整的,有些架包忘删了导致项目有点大。
Struts上传,已经处理好乱码问题Struts上传,已经处理好乱码问题
1.struts2 带进度条文件上传 源码 2.源码中有lib 下载后部署马上就能用 3.代码中有详细的注释信息 4.对关键点有详细的解释说明 5.如果谁用起来觉得有问题可在评论留言,留联系方式,我会联系你的 6.非常感谢原创作者...
文件上传、多文件上传,文件名 乱码,上传文件名的重名都在此工程中有具体的解决 此工程中为了上传容易,删除了jar包,要使的工程运行,只要加入struts-1.2.9的jar包就可以了
struts文件上传单元目录列表: 01.文件上传的网页设置与数据格式分析 02.阐释软件分层的设计思想 03.文件上传组件的实现原理与应用分析 04.用struts获取上传文件名和解决中文乱码 05.保存用struts获取的上传文件内容...
struts文件上传单元目录列表: 01.文件上传的网页设置与数据格式分析 02.阐释软件分层的设计思想 03.文件上传组件的实现原理与应用分析 04.用struts获取上传文件名和解决中文乱码 05.保存用struts获取的上传文件内容...
struts文件带进度条上传,文件下载 解决个浏览器中文乱码,带进度条,同时下载页支撑各个浏览器
Struts2文件 上传 下载 解决: a标签中文乱码,无法下载问题 tomcat下载格式变换问题,如下载apk,下载下来的则是zip
Struts2+hibernate+spring实现多文件的上传与下载功能,主要包括:上传、下载、删除 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.Struts2进行下载处理,能对上传的所有文件进行...
在网上找了半天,看见两偏整合教程不错,把两偏整理成一偏供参考。 别人一个文档就要10分才能下,本人分不多了,扣大家5分来赚取回来。...Ckeditor3.X与ckfinder2.X整合forJAVA (支持文件上传,解决中文乱码)
struts文件的上传下载功能实现,并利用过滤器解决了相应的中文乱码问题
文件上传一直是Ext3.0以上版本的难点,本实例利用Ext3.2和struts实现文件和参数的共同上传,并解决了中文乱码的问题。
一个比较简单的struts项目,实现了文件的上传,文件的中中文乱码的处理,里面有很多细节值得注意