|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.io.FileInputStream; | 
|---|
|  |  |  | import java.net.URLEncoder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * web文件下载 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("/down") | 
|---|
|  |  |  | public void down(String id, HttpServletRequest req, HttpServletResponse rep){ | 
|---|
|  |  |  | OthFile fPo = sv.selectById(id) ; | 
|---|
|  |  |  | if(fPo != null){ | 
|---|
|  |  |  | FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ; | 
|---|
|  |  |  | if(frVo.fileSysAbsolutePath != null){ | 
|---|
|  |  |  | if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){ | 
|---|
|  |  |  | frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ; | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | OthFile fPo = sv.selectById(id) ; | 
|---|
|  |  |  | if(fPo != null){ | 
|---|
|  |  |  | FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ; | 
|---|
|  |  |  | if(frVo.fileSysAbsolutePath != null){ | 
|---|
|  |  |  | if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){ | 
|---|
|  |  |  | frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String filePath = frVo.fileSysAbsolutePath + fPo.filePath ; | 
|---|
|  |  |  | File f = new File(filePath) ; | 
|---|
|  |  |  | if(f.exists()){ | 
|---|
|  |  |  | String fileReName = fPo.orgName + "." + fPo.extName ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | fileReName = new String(fileReName.getBytes("UTF-8"), "ISO-8859-1"); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | fileReName = "file" ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | rep.addHeader("content-type", "application/octet-stream"); | 
|---|
|  |  |  | rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); | 
|---|
|  |  |  | String filePath = frVo.fileSysAbsolutePath + fPo.filePath ; | 
|---|
|  |  |  | File f = new File(filePath) ; | 
|---|
|  |  |  | if(f.exists()){ | 
|---|
|  |  |  | String fileReName = fPo.orgName + "." + fPo.extName ; | 
|---|
|  |  |  | //URLEncoder.encode可以防止中文乱码 | 
|---|
|  |  |  | fileReName = URLEncoder.encode(fileReName, "UTF-8").replaceAll("\\+", "%20"); | 
|---|
|  |  |  | rep.addHeader("content-type", "application/octet-stream"); | 
|---|
|  |  |  | rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ServletOutputStream out = null; | 
|---|
|  |  |  | FileInputStream in = null ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | out = rep.getOutputStream() ; | 
|---|
|  |  |  | } catch (Exception ee) { | 
|---|
|  |  |  | out = null ; | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | if(out != null){ | 
|---|
|  |  |  | byte[] bs = new byte[1024] ; | 
|---|
|  |  |  | int len = -1 ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | in = new FileInputStream(f); | 
|---|
|  |  |  | len = in.read(bs) ; | 
|---|
|  |  |  | while(len != -1){ | 
|---|
|  |  |  | out.write(bs, 0, len); | 
|---|
|  |  |  | ServletOutputStream out = null; | 
|---|
|  |  |  | FileInputStream in = null ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | out = rep.getOutputStream() ; | 
|---|
|  |  |  | } catch (Exception ee) { | 
|---|
|  |  |  | out = null ; | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | if(out != null){ | 
|---|
|  |  |  | byte[] bs = new byte[1024] ; | 
|---|
|  |  |  | int len = -1 ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | in = new FileInputStream(f); | 
|---|
|  |  |  | len = in.read(bs) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception eee) { | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | if(out != null){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | out.flush(); | 
|---|
|  |  |  | out.close(); | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | if(in != null){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | in.close(); | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | while(len != -1){ | 
|---|
|  |  |  | out.write(bs, 0, len); | 
|---|
|  |  |  | len = in.read(bs) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception eee) { | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | if(out != null){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | out.flush(); | 
|---|
|  |  |  | out.close(); | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | if(in != null){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | in.close(); | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.error("下载文件时异常", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|