struts2多文件上传下载(解决中文乱码)
1:struts.xml
<action name="fileUtilAction" + 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;
}