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多文件上传下载(解决中文乱码)
猜你喜欢
转载自wawa129.iteye.com/blog/1663508
今日推荐
周排行