Java后台上传下载文件的几种方式
public void uploadImage ( String localImagePath) throws Exception { try { File imageFile = new File ( localImagePath) ; if ( imageFile. exists ( ) ) { String url = "http://" + mapVendor. getHost ( ) + mapVendorImageDetail. getBackUrl ( ) ; HttpHeaders headers = new HttpHeaders ( ) ; headers. set ( "Content-Type" , "image/jpeg" ) ; headers. add ( "Host" , mapVendor. getHost ( ) ) ; headers. add ( "Authorization" , mapVendorImageDetail. getBackAuth ( ) ) ; restTemplate. put ( url, new HttpEntity < Resource > ( new FileSystemResource ( imageFile) , headers) ) ; } } catch ( RestClientException e) { logger. error ( "send local image [" + localImagePath + "] to tencent error" , e) ; }
}
public void download ( String path, HttpServletResponse response) { try { File file = new File ( path) ; String filename = file. getName ( ) ; String ext = filename. substring ( filename. lastIndexOf ( "." ) + 1 ) . toUpperCase ( ) ; InputStream inputStream = new BufferedInputStream ( new FileInputStream ( file) ) ; byte [ ] buffer = new byte [ inputStream. available ( ) ] ; inputStream. read ( buffer) ; inputStream. close ( ) ; response. reset ( ) ; response. setContentType ( "application/octet-stream" ) ; response. addHeader ( "Content-Disposition" , "attachment;filename=" + new String ( filename. getBytes ( ) ) ) ; response. addHeader ( "Content-Length" , "" + file. length ( ) ) ; OutputStream outputStream = new BufferedOutputStream ( response. getOutputStream ( ) ) ; outputStream. write ( buffer) ; outputStream. flush ( ) ; outputStream. close ( ) ; } catch ( IOException ex) { }
public void downloadNet ( ) throws Exception { int byteSum = 0 ; int byteRead; URL url = new URL ( "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" ) ; URLConnection conn = url. openConnection ( ) ; InputStream inputStream = conn. getInputStream ( ) ; FileOutputStream fileOutputStream = new FileOutputStream ( "c:/logo.gif" ) ; byte [ ] buffer = new byte [ 1204 ] ; while ( ( byteRead = inputStream. read ( buffer) ) != - 1 ) { byteSum += byteRead; System . out. println ( byteSum) ; fileOutputStream. write ( buffer, 0 , byteRead) ; }
}
public void downLoad ( String filePath, HttpServletResponse response, boolean isOnLine) throws Exception { File f = new File ( filePath) ; if ( ! f. exists ( ) ) { response. sendError ( 404 , "File not found!" ) ; return ; } BufferedInputStream br = new BufferedInputStream ( new FileInputStream ( f) ) ; byte [ ] buf = new byte [ 1024 ] ; int len = 0 ; response. reset ( ) ; if ( isOnLine) { URL u = new URL ( "file:///" + filePath) ; response. setContentType ( u. openConnection ( ) . getContentType ( ) ) ; response. setHeader ( "Content-Disposition" , "inline; filename=" + f. getName ( ) ) ; } else { response. setContentType ( "application/x-msdownload" ) ; response. setHeader ( "Content-Disposition" , "attachment; filename=" + f. getName ( ) ) ; } OutputStream out = response. getOutputStream ( ) ; while ( ( len = br. read ( buf) ) > 0 ) out. write ( buf, 0 , len) ; br. close ( ) ; out. close ( ) ; }