Java下载指定内容的文件到本地
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileUtils;
@PostMapping(value = "/download/file")
public void downFileAuthor(HttpServletResponse response) {
FileInputStream fis = null;
try {
String path = "user.lic";
List<String> list = new ArrayList<>();
list.add("用户名:username");
list.add("密码:password");
File file = new File(path);
FileUtils.writeLines(file, list);
fis = new FileInputStream(file);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"), "ISO8859-1"));
OutputStream os = response.getOutputStream();
IOUtils.copy(fis, os);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
还没有评论,来说两句吧...