Struts2实现文件下载功能代码分享(文件名中文转码)

网友投稿 171 2023-07-15


Struts2实现文件下载功能代码分享(文件名中文转码)

struts2文件下载功能实现代码如下所示:

Action文件

public class DownLoadAction extends ActionSupport {

/**

*

*/

private static final long serialVersionUID = 5879762231742395104L;

private String fileName;//用户请求的文件名

private String inputPath;//下载资源的路径(在struts配置文件中设置)

public void setInputPath(String inputPath) {

this.inputPath = inputPath;

}

public String getInputPath() {

return inputPath;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

public String getFileName() {

return fileName;

}

public String downloadFile() throws Exception {

ServletContext context = ServletActionContext.getServletContext();

String downloadDir = context.getRealPath("/upload");

String downloadFile = context.getRealPath(inputPath);

//防止用户请求不安全的资源

if(!downloadFile.startsWith(downloadDir)) {

return null;

}

return "download_success";

}

/*

* 获取输入流资源

*/

public InputStream getInputStream() throws Exception {

String path = inputPath + File.separatorhttp://Char + new String(fileName.getBytes("ISO-8859-1"), "UTF-8");

return ServletActionContext.getServletContext().getResourceAsStream(path);

}

/*

* 获取下载时文件默认的文件名

*/

public String getDownloadFileName() {

String downloadFileName = fileName;

try {

downloadFileName = URLEncoder.encode(downloadFileName, "ISO-8859-1");

} catch (UnsupportedEncodingException e) {

e.getMessage();

e.printStackTrace();

}

return downloadFileName;

}

}

jsp文件:

dwr.jar文件下在

Struts2教程.pdf文http://件下在

struts.xml:

/upload

application/octet-stream

inputStream

attachment;filename="${downloadFileName}"

以上所述是给大家介绍的Struts2实现文件下载功能代码分享(文件名中文转码)的相关内容,希望对大家有所帮助!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring加载properties文件的方法
下一篇:SSH框架网上商城项目第15战之线程、定时器同步首页数据
相关文章

 发表评论

暂时没有评论,来抢沙发吧~