JAVA后台实现文件下载
使用springboot框架,我在本地项目中放了一个文件,然后前台来下载该文件。本人遇到的比较明显的问题:
-
spring获取当前项目路径
String downloadPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
-
下载路径中文乱码下载路径中文乱码
解决方法:filePath=URLDecoder.decode(downloadPath,"UTF-8")
-
下载后文件名称中文乱码
解决方法:String name = new String(name.getBytes("GBK"), "ISO-8859-1");
-
下载路径分隔符
使用分隔符:String path = realPath + File.separator + name;
具体执行代码:
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
@ResponseBody
public void downloadFile(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name ="某某软件.exe";
String downloadPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
// 进行转码后的文件名,用来下载之后的文件名
download(resp,name,downloadPath);
}
/**
* @param resp
* @param name 文件真实名字
* @param downloadName 文件下载时名字
* @throws IOException
*/
public static void download(HttpServletResponse resp, String name, String downloadPath) throws IOException {
String filePath = null;
try {
filePath=URLDecoder.decode(downloadPath,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//String realPath = "D:" + File.separator + "apache-tomcat-8.5.15" + File.separator + "files";
String realPath=filePath+"static"+File.separator +"software";//项目路径下你存放文件的地方
String path = realPath + File.separator + name;//加上文件名称
File file = new File(path);
if(!file.exists()){
throw new IOException("文件不存在");
}
name = new String(name.getBytes("GBK"), "ISO-8859-1");
resp.reset();
resp.setContentType("application/octet-stream");
resp.setCharacterEncoding("utf-8");
resp.setContentLength((int) file.length());
resp.setHeader("Content-Disposition", "attachment;filename=" + name);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = resp.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = 0;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
补充:
1.前台页面
window.open(url);
url里面放上地址就行了
2.然而后来部署的时候遇到了些问题,springboot会把文件打成jar包,所以这样的路径会带有!,所以一般找不到路径,后来在一位前辈的帮助下,把File改成了InputStream,代码如下:
@ResponseBody
@RequestMapping(value = "/downloadFile")
public void downloadFile(HttpServletResponse response) throws IOException {
InputStream f= this.getClass().getResourceAsStream("/static/AA/控件.exe");
response.reset();
response.setContentType("application/x-msdownload;charset=utf-8");
try {
response.setHeader("Content-Disposition", "attachment;filename="+ new String(("控件" + ".exe").getBytes("gbk"), "iso-8859-1"));//下载文件的名称
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletOutputStream sout = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(f);
bos = new BufferedOutputStream(sout);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
bos.close();
bis.close();
} catch (final IOException e) {
throw e;
} finally {
if (bis != null){
bis.close();
}
if (bos != null){
bos.close();
}
}
}
```