2023.3.02
项目管理系统中对项目文档的处理
本来全部设置的是二进制流,发现下载后没有文件类型和文件名称
对应的解决办法:
上传、下载的时候对文件类型进行说明
上传:
private String upload(String existLobId, String contentType, MultipartFile multiFile) throws RuntimeException {
String lobId = null;
try {
byte[] all = multiFile.getBytes();
//如果已有lobId则删除原文档
if (StringUtils.isNotBlank(existLobId)) {
lobDbServiceClient.delete(existLobId);
}
lobId = lobDbServiceClient.add(all, contentType, "/xmgl/doc").getSuccRsValue();
} catch (IOException e) {
log.error("附件存小文件系统异常", e);
throw new RuntimeException("附件存小文件系统异常");
}
return lobId;
下载:
public HttpServletResponse download(String id, HttpServletResponse response) {
DocData docData = getDocById(id);
FileOutputStream fileOutputStream = null;
ServletOutputStream outStream = null;
try {
// 写成文件
String fileName = docData.getFileName();
String contentType = "application/octet-stream";
RetVal val = getContentType(fileName);
if (val.isSuccess()) {
contentType = val.getValue().toString();
}
File file = new File(fileName);
fileOutputStream = new FileOutputStream(file);
// 以流的形式下载文件。
byte[] fileContent = lobDbServiceClient.getLob(docData.getLobId()).getSuccRsValue().getContent();
fileOutputStream.write(fileContent);
fileOutputStream.close();
// 设置response的Header
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + fileContent.length);
//输出流文件
outStream = response.getOutputStream();
InputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
fis.close();
} catch (IOException ex) {
log.error("文件读取异常", ex);
ex.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response;
}
/**
* 根据不同文件类型设置contentType
*/
private RetVal getContentType(String fileName) {
String contentType = null;
String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
switch (ext) {
case "html":
case "htm":
contentType = "text/html; charset=utf-8";
break;
case "txt":
contentType = "text/plain; charset=utf-8";
break;
case "pdf":
contentType = "application/pdf";
break;
case "doc":
contentType = "application/msword";
break;
case "docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case "xls":
contentType = "application/vnd.ms-excel";
break;
case "xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "ppt":
contentType = "application/vnd.ms-powerpoint";
break;
case "pptx":
contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
break;
case "wps":
contentType = "application/vnd.ms-works";
break;
case "xml":
contentType = "application/xml; charset=utf-8";
break;
case "gif":
contentType = "image/gif";
break;
case "jpg":
case "jpeg":
contentType = "image/jpeg";
break;
case "png":
contentType = "image/png";
break;
case "zip":
contentType = "application/zip";
break;
case "tar":
contentType = "application/x-tar";
break;
case "gz":
contentType = "application/gzip";
break;
case "mp3":
contentType = "audio/mpeg";
break;
case "mp4":
contentType = "video/mp4";
break;
case "md":
contentType = "text/markdown";
break;
case "rp":
contentType = "application/x-rp";
break;
}
if (StringUtils.isNotBlank(contentType)) {
return RetVal.success(contentType);
}
return RetVal.error("不支持该文件类型!");
}