用Struts2实现上传下载需要加上以下两个jar包(版本自选)
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
1:Struts.xml配置
<!-- 设置全局上传文件的大小 -->
<constant name="struts.multipart.maxSize" value="90000000"/>
<!-- 国际化 -->
<constant name="struts.custom.i18n.resources" value="/i18n/messages" />
<!-- 定义系统设置模块 -->
<package name="biz" extends="base" namespace="/biz">
<!--系统设置 -->
<action name="*_*"
class="com.mrp.action.{1}Action" method="{2}">
<result name="success">${forwardView}</result>
<result name="error">${forwardView}</result>
<result name="input">/error/error.jsp</result>
<!-- 限制文件上传大小和类型 -->
<interceptor-ref name="fileUpload">
<param name="maximumSize">50000000</param>
<!-- <param name="allowedTypes">
application/msword,application/pdf,application/zip
</param> -->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 文件的下载 -->
<result name="download" type="stream">
<!-- 文件类型 —— application/octet-stream 表示无限制 -->
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!-- 流对象名 —— 去找Action中的getInputStream方法 -->
<param name="inputName">inputStream</param>
<!-- 文件名 —— 将调用该Action中的getDownloadFileName方法 -->
<param name="contentDisposition">attachment;fileName="${downloadFileName}"</param>
<!-- <param name="bufferSize">4096</param> -->
</result>
</action>
</package>
2:action类
/**
* 涉及文件的上传下载的变量
*/
private File photo;
private String photoFileName;
private String photoContentType;
private String savePath;
private String fileName;//要下载的文件名
private InputStream inputStream;
/**
* 添加课程材料(包括上传文件的功能)
* @return
* @throws Exception
* @author uug
* @date 2017年12月4日
*/
public String add() throws Exception{
System.out.println("**********************upload*********************");
//判断目录是否存在,不存在的话要新建
File file = new File(getSavePath());
if(!file.exists() && !file.isDirectory()){
System.out.println(savePath+"目录不存在,需要重建");
file.mkdir();
}
//创建目标文件对象,文件名PhotoFileName,格式_FileName
File destFile=new File(getSavePath(),getPhotoFileName());
//把上传的文件,拷贝到目标文件中
FileUtils.copyFile(getPhoto(), destFile);
System.err.println("文件名称"+getPhotoFileName());
//将前台转来的数据赋值给新的对象 tSbject.set新的值进去保存会出错
TSubject tSubject1 = new TSubject(tSubject.getKeNum(),tSubject.getKeName(),
tSubject.getSubject(), tSubject.getSubjectDec(), getPhotoFileName(), tSubject.getTeacher());
subjectService.add(tSubject1);
tSubject1.setKeNum(null);
tSubject1.setSubject(null);
return list2(tSubject.getTeacher());
}
/**
* 文件的下载
* @return
* @throws Exception
* @author uug
* @date 2017年12月20日
*/
public String download(){
System.out.println("********************download****************");
System.out.println("getFileName="+getFileName());
inputStream = ServletActionContext.getServletContext().getResourceAsStream("/upload/" + getFileName());
System.out.println("inputStream="+inputStream);
return "download";
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath("/upload");
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
//为解决文件名字乱码。struts2.xml中读取的是downloadFileName而不是fileName
public String getDownloadFileName() throws UnsupportedEncodingException {
String downloadFileName = new String(fileName.getBytes(), "ISO8859-1");
return downloadFileName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
//this.fileName = fileName;
this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
3:jsp
上传:
<li><label>附件</label><input type="file" name="photo" id="photo"/></li>
下载:
<a href="<%= basePath%>/biz/Subject_download.action?fileName=${attachment}" class="tablelink">下载</a>
4:国际化
struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\uFF0C\u8BF7\u91CD\u65B0\u4E0A\u4F20\uFF01
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u6B63\u786E\uFF0C\u4E0A\u4F20\u5931\u8D25\uFF01