如何将下载的文件重命名并保存?

2026-04-15 08:3310阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计239个文字,预计阅读时间需要1分钟。

如何将下载的文件重命名并保存?

文件下载(文件重命名)+ @Override public void downloadFile(Long id, HttpServletResponse response) { SysFileLib sysFileLib=findOne(id); if (sysFileLib==null) { logger.error(对象不存在,fileLibId: + id); String message=ServiceM; } }

如何将下载的文件重命名并保存?

文件下载(文件重命名)

@Override public void downloadFile(Long id, HttpServletResponse response) { SysFileLib sysFileLib = findOne(id); if(sysFileLib==null){ logger.error("对象不存在,fileLibId:" + id); String message = ServiceManager.sysExceptionCodeService.getMessageByCode(ResGlobalExt.COMMON_OBJECT_NO_FOUND); throw new BusinessException(ResGlobalExt.COMMON_OBJECT_NO_FOUND, message); } File file = FileSystemEngine.getFileSystem().getLocalFile(sysFileLib.getSysFileId()); if(!file.exists()){ logger.error("文件不存在,fileNm:" + sysFileLib.getFileNm()); String message = ServiceManager.sysExceptionCodeService.getMessageByCode(ResGlobalExt.COMMON_OBJECT_NO_FOUND); throw new BusinessException(ResGlobalExt.COMMON_OBJECT_NO_FOUND, message); } FileInputStream fis = null; OutputStream os = null; try { fis = new FileInputStream(file); String sysFileId = sysFileLib.getSysFileId(); String exName = sysFileId.substring(sysFileId.lastIndexOf(".")); String filename = new String((sysFileLib.getFileNm() + exName).getBytes("GBK"), "ISO-8859-1"); response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.addHeader("Content-Length", "" + file.length()); os = response.getOutputStream(); byte[] buffer = new byte[1024]; int temp = 0; while ((temp = fis.read(buffer))!=-1){ os.write(buffer, 0, temp); } os.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }

本文共计239个文字,预计阅读时间需要1分钟。

如何将下载的文件重命名并保存?

文件下载(文件重命名)+ @Override public void downloadFile(Long id, HttpServletResponse response) { SysFileLib sysFileLib=findOne(id); if (sysFileLib==null) { logger.error(对象不存在,fileLibId: + id); String message=ServiceM; } }

如何将下载的文件重命名并保存?

文件下载(文件重命名)

@Override public void downloadFile(Long id, HttpServletResponse response) { SysFileLib sysFileLib = findOne(id); if(sysFileLib==null){ logger.error("对象不存在,fileLibId:" + id); String message = ServiceManager.sysExceptionCodeService.getMessageByCode(ResGlobalExt.COMMON_OBJECT_NO_FOUND); throw new BusinessException(ResGlobalExt.COMMON_OBJECT_NO_FOUND, message); } File file = FileSystemEngine.getFileSystem().getLocalFile(sysFileLib.getSysFileId()); if(!file.exists()){ logger.error("文件不存在,fileNm:" + sysFileLib.getFileNm()); String message = ServiceManager.sysExceptionCodeService.getMessageByCode(ResGlobalExt.COMMON_OBJECT_NO_FOUND); throw new BusinessException(ResGlobalExt.COMMON_OBJECT_NO_FOUND, message); } FileInputStream fis = null; OutputStream os = null; try { fis = new FileInputStream(file); String sysFileId = sysFileLib.getSysFileId(); String exName = sysFileId.substring(sysFileId.lastIndexOf(".")); String filename = new String((sysFileLib.getFileNm() + exName).getBytes("GBK"), "ISO-8859-1"); response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.addHeader("Content-Length", "" + file.length()); os = response.getOutputStream(); byte[] buffer = new byte[1024]; int temp = 0; while ((temp = fis.read(buffer))!=-1){ os.write(buffer, 0, temp); } os.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }