1.概述
c语言好像不能直接下载文件(一个做java不知道,苦笑。。。),所以让我转发文件,其实就是让我读取流,然后将流写出。
采用SpringMVC+Spring框架
2.文件名称和类型怎么确定呢
浏览器它也不知道这个是什么类型,只知道是字节流,那么浏览器大哥他有自己判断标准,也就是最后一个"/"整个部分
@ResponseBody
@RequestMapping(value="/download/1.jpg", method = RequestMethod.GET)
public void download( HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/octet-stream");
String dir = System.getProperty("catalina.home"); //获得tomcat所在的工作路径
System.out.println("tomcat路径=" + dir);
//获取到存储了文件存储位置的filedir.properties 文件路径
String dir2 = dir.substring(0, dir.length()) + File.separator +"webapps" + File.separator + "ROOT" + File.separator + "1.jpg";
File file = new File(dir2);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
InputStream inputStream = new FileInputStream(file);
while ((len = inputStream.read(buffer)) > -1 ) {
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.flush();
response.getOutputStream().write(byteArrayOutputStream.toByteArray());
} catch (FileNotFoundException e) {
logger.error("读取文件异常", e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
logger.info("下载进入。。。。。。。。。。。。。。。。。");
}
总结:下载文件名为1.jpg,这个就表明是一个图片格式,刚开始时候没有后面/1.jpg,所以它显示的是download为文件名。