您的位置:首页 > 编程语言 > Java开发

Java_文件上传与下载(三)

2016-08-11 19:15 387 查看

Struts2实现

1.原理

a.表单中设置为post提交,encytype属性设置为multipart/form-dat,这样数据就以二进制流的方式传输
b.Struts2默认采用Jakarta和Common-FileUpload框架,导入相应jar包即可
c.通过Struts2的拦截器设置上传文件的类型及大小等
d.在Struts2的action中设置上传文件的内容、类型及名称


2.文件上传

(1)Action的写法:

public class FileUploadAction extends ActionSupport {

private File upload;    //这里必须与表单中的input的Name一致
private String uploadContentType;
private String uploadFileName;

private String result;

//省略set/get方法
。。。。。。
。。。。。。

//Struts2的action中excute方法的覆写
@Override
public String execute() throws Exception {

String filePath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
FileUtils.copyFile(upload, new File(file,uploadFileName));
result = "上传成功!";

return SUCCESS;
}
}


(2)Struts2的xml配置:*

<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.mystudy.action.FileUploadAction">
<result>/jsps/struts2upload.jsp</result>
<result name="input">/jsps/error.jsp</result>
<!-- 拦截器配置 -->
<interceptor-ref name="fileUpload">
<!-- 允许上传的文件类型 -->
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
<!-- 允许上传的最大文件 -->
<param name="maximumSize">10M</param>
</interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>


(3)异常处理:

引入struts2的国际化的支持,在xml中添加
<constant name="struts.custom.i18n.resources" value="resource"/>
资源文件:假设定义了resouce_zh_CN.properties的文件


(4)批量上传:

a.jsp中多加几个input

上传文件1:<input type="file" name="upload"/><br>
上传文件2:<input type="file" name="upload"/><br>
上传文件3:<input type="file" name="upload"/><br>

b.Action中的成员定义为List

private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;

c.保存文件时循环逐个保存


(2)文件下载

(1)Struts2的xml配置:

<!-- 文件下载的action配置 -->
<action name="download" class="com.mystudy.action.FileDownloadAction">
<param name="inputPath">/images/image2.jpg</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">8192</param>
</result>
</action>


(2)Action的写法:

private String inputPath;
public String filename;

public String getInputPath() {
return inputPath;
}

public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}

@Override
public String execute() throws Exception {

return SUCCESS;
}

public InputStream getInputStream() throws IOException{

String path =  ServletActionContext.getServletContext().getRealPath("/images");
String filePath = path+"\\"+ filename;
File file = new File(filePath);
return FileUtils.openInputStream(file);
//return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}

public String getDownloadFileName(){
String downloadFileName = "";
try {
downloadFileName=URLEncoder.encode("下载文件.jpg","UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java struts