spring java 获取webapp下文件路径
来源:互联网 发布:艾默生网络能源 江门 编辑:程序博客网 时间:2024/06/05 10:54
自己的代码-(无法找到文件):
主要问题在于无法找到相应的文件,各位游客如果看到我的代码中有哪些问题请勇敢大声的指出来:
@RequestMapping("showAttachmentPDF")
public String showAttachmentPDF(Model model,HttpServletRequest req) {
HttpServletRequest request = TrustWebUtils.getRequest();
HttpServletResponse response = TrustWebUtils.getReponse();
String fileName = request.getParameter("fileName");
String path = request.getContextPath();// etrading
String path1="/resources/doc/protocol.pdf";
path += path1;
response.setContentType("application/pdf");
FileInputStream in;
OutputStream out;
try {
in = new FileInputStream(new File(path));
out = response.getOutputStream();
byte[] b = new byte[1024 * 8];
while ((in.read(b)) != -1) {
out.write(b);
}
out.flush();
in.close();
out.close();
} catch (FileNotFoundException e) {
throw new BusinessException("",MessageUtils.getMessage("找不到文件"));
} catch (IOException e) {
throw new BusinessException(MessageUtils.getMessage(ErrorCodes.FILE_DOWN_EXCEPTION));
}
return null;
}
网上摘抄的代码-(亲测可用):
【出处】http://blog.csdn.net/fuyesunwang123/article/details/30241341
@RequestMapping("/act/worldcup_schedule_time/imgdownload")
@ResponseBody
public String scheduleDownload(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
response.setCharacterEncoding("UTF-8");
String downLoadName = "worldcup.jpg";
InputStream input = null;
try {
request.setCharacterEncoding("UTF-8");
//获取文件的路径
// String url = session.getServletContext().getRealPath("/") + "resources\\images\\act\\worldcup_merge\\worldcup720.png";
String url = session.getServletContext().getRealPath("/") + "resources/images/act/worldcup_merge/worldcup720.png";
System.out.println(url);
File file = new File(url);
input = FileUtils.openInputStream(file);
byte[] data = IOUtils.toByteArray(input);
//System.out.println("文件名:"+downLoadName);
response.reset();
//设置响应的报头信息(中文问题解决办法)
response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(downLoadName, "UTF-8"));
response.addHeader("Content-Length", "" + data.length);
response.setContentType("image/png; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
} catch (Exception e) {
logger.error("下载图片出错");
if (input != null) {
IOUtils.closeQuietly(input);
}
}
return null;
}
- spring java 获取webapp下文件路径
- spring java 获取webapp下文件路径
- java获取指定路径下文件的方法
- 获取多语言下文件路径
- res/raw下文件路径获取
- OC获取folder下文件的路径
- C++ 获取路径下文件、文件夹
- 获取assets目录下文件的路径
- java 之 classpath下文件路径
- java监控路径下文件变化
- java获取web工程下文件的路径,如配置文件properties
- java获取工程目录下文件
- java获取文件夹下文件的数目
- java获取工程目录下文件
- 路径下文件遍历
- 程序获取eclipse工程下文件路径的解决方法
- android 如何获取 assets 下文件的路径
- C#获取指定路径下文件,并按时间排序
- Appium的DesiredCapabilities参数设置
- 给按钮设置按下抬起状态
- Android Parcelable和Serializable的区别
- Cocoa Touch事件处理流程
- 笔试面试题
- spring java 获取webapp下文件路径
- Mozilla新特性只支持https网站,再次推动SSL证书普及
- 充分统计量
- Flex3与java BlazeDS入门教程及其配置详解
- PPPoE简介
- SecureCRT ftp操作
- MSBuild编译vs工程的常用指令
- Matlab 读写入txt文件
- 电脑显卡4种接口类型:VGA、DVI、HDMI、DP