比如文件上传在OSS系统上面,那么我怎么让客户端下载到本地?
Services实现层:
attachment绝对路径,比如:https://imgsrc.baidu.com/baike/pic/item/dcc451da81cb39db5aacb0c8dc160924aa1830c4.jpg
public void fileDownload(HttpServletRequest request, HttpServletResponse response, String attachment) {
// 获取上下文对象
ServletContext servletContext = request.getSession().getServletContext();
// 获取文件资源的路径
String path = attachment;//servletContext.getRealPath(Constants.REPORT_REPORTFILEPATH);
log.debug(path);
URL url = null;
InputStream openStream = null;
try (OutputStream fos = response.getOutputStream();) {
url = new URL(attachment);
openStream = url.openStream();
String mimeType = servletContext.getMimeType(attachment);
if (mimeType == null) {
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
String headerkey = "Content-Disposition";
String fileName = attachment.substring(attachment.lastIndexOf("/") + 1);
String headerValue = String.format("attachment;filename=\"%s\"", fileName);
response.setHeader(headerkey, headerValue);
byte buff[] = new byte[BUFF_SIZE];
int flag = -1;
while ((flag = openStream.read(buff)) != -1) {
fos.write(buff, 0, flag);
}
} catch (Exception e) {
e.printStackTrace();
log.error("ProjectDetailServiceImpl fileDownload() have an exception {} = ", e);
} finally {
try {
if (openStream != null) {
openStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}