struts2多文件上传下载(解决中文乱码)
1:struts.xml
<actionname="fileUtilAction"class="com.asia.home.cn.web.announce.FileUtilAction">
<resultname="input">/main/announce2.0/error.jsp</result>
<paramname="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
<paramname="savePath">/annnounceUpload</param>
<resulttype="stream">
<paramname="contentType">application/octet-stream;charset=ISO8859-1</param>
<paramname="inputName">inputStream</param>
//fileName下载时弹出窗显示的文件名。与action中的fileName属性对应
<paramname="contentDisposition">attachment;filename="${fileName}"</param>
<paramname="bufferSize">20096</param>
</result>
</action>
2:action
packagecom.asia.home.cn.web.announce;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.PrintWriter;
importjava.io.UnsupportedEncodingException;
importorg.apache.struts2.ServletActionContext;
importorg.apache.struts2.interceptor.ServletRequestAware;
importorg.apache.struts2.interceptor.ServletResponseAware;
importorg.apache.struts2.interceptor.SessionAware;
importcom.asia.home.cn.base.AsiaBaseAction;
importcom.asia.home.cn.utils.UrlUtils;
publicclassFileUtilActionextendsAsiaBaseActionimplementsSessionAware,
ServletRequestAware,ServletResponseAware{
privatestaticfinallongserialVersionUID=1L;
privateStringfileName;//下载用
privateStringtitle;
privateFile[]upload;
privateString[]uploadContentType;
privateString[]uploadFileName;
//接受依赖注入的属性
privateStringsavePath;
//接受依赖注入的方法
publicvoidsetSavePath(Stringvalue)
{
this.savePath=value;
}
privateStringgetSavePath()throwsException
{
returnrequest.getRealPath(savePath);
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicvoidsetUploadContentType(String[]uploadContentType){
this.uploadContentType=uploadContentType;
}
publicvoidsetUploadFileName(String[]uploadFileName){
this.uploadFileName=uploadFileName;
}
publicStringgetTitle(){
return(this.title);
}
publicFile[]getUpload(){
returnupload;
}
publicvoidsetUpload(File[]upload){
this.upload=upload;
}
publicString[]getUploadContentType(){
return(this.uploadContentType);
}
publicString[]getUploadFileName(){
return(this.uploadFileName);
}
//上传
publicvoiduploadFile()
{
File[]files=getUpload();
FileInputStreamfis=null;
FileOutputStreamfos=null;
PrintWriterout=null;
try{
for(inti=0;i<files.length;i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
out=response.getWriter();
Filefile=newFile(getSavePath());
if(!file.exists()){
file.mkdir();
}
fos=newFileOutputStream(getSavePath()+"\\"+getUploadFileName()[i]);
fis=newFileInputStream(files[i]);
byte[]buffer=newbyte[10024];
intlen=0;
while((len=fis.read(buffer))>0)
{
fos.write(buffer,0,len);
}
}
out.write("true");
}
catch(Exceptione){
out.write("false");
e.printStackTrace();
}
finally{
try{
fos.close();
fis.close();
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
publicInputStreamgetInputStream(){
try{
//如果没有以下这句话,下载窗口中的中文名就乱码了。。。。
response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(fileName,"UTF-8"));
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
returnServletActionContext.getServletContext().getResourceAsStream("/annnounceUpload/"+fileName);
}
//下载
publicStringdownLoad(){
return"success";
}
publicvoidsetFileName(StringfileName){
this.fileName=UrlUtils.decode(UrlUtils.decode(fileName,"utf-8"),"utf-8");
}
publicStringgetFileName(){
returnfileName;
}
}
3:html
上传:
<formaction="fileUtilAction!uploadFile.action"method="post"id="form1"name="form1"enctype="multipart/form-data">
<inputtype='hidden'name="<%=TokenUtils.TOKEN_STRING_NAME%>"value="<%=sessionToken%>"/>
<tableid="myTable"width="100%"cellpadding="3"border=1style="border:1pxsolid#cccccc;line-height:30px;font-size:13px;BACKGROUND-COLOR:#fefeed;BORDER-COLLAPSE:collapse;">
<tralign="center">
<tdcolspan="4">文件1:<inputid="sd"type="checkbox"/><inputtype="file"style="width:200px;"id="fileName"name="upload"/></td>
</tr>
</table>
<table>
<tr>
<tdalign="center">
<inputtype='button'style='border:1pxsolid#000;cursor:pointer;background:#ff6600;color:#fff;'onclick="submitForm();"value='保存'/>
<inputtype='button'style="border:1pxsolid#000;cursor:pointer;background:#ff6600;color:#fff"value='取消'onclick="javascript:window.close();"style='cursor:hand;'/>
<inputtype="button"value="添加一行"id="newBtn"/>
<inputtype="button"value="删除一行"id="delBtn"/>
</td>
</tr>
</table>
</form>
下载:
functiondownload(){
varauditStatus=encodeURI(encodeURI("SQL高级.doc"));
window.location.href="fileUtilAction!downLoad.action?fileName="+auditStatus;
}