跨服務器上傳文件的方式有很多,其中一種是使用在中間服務器上使用臨時文件的方式進行保存后再發送到另一個服務器上,實現文件上傳。
問題點:中間保存臨時文件,還需要不定時的進行文件清理,比較麻煩
直接進行文件的轉發,使用byte[]數組方式直接進行文件轉發,然后,服務器根據傳遞的byte[]數組進行轉文件方式,使用httpclient方式將byte[]數組發送到服務端,特別注意的點在於,
發送的時候使用"content-type" = "application/json"發送到服務端,服務端使用@RequestBody byte[] bytes 進行方法接收,其他的一些參數如果還要傳遞,可以使用header將參數傳遞過去
package com.rainy.demo.utils;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import java.io.IOException; /** * Created by rainy on 2016/12/17. */ public class UpImgUtils { public static String getPath(String url, String fileType, byte[] bytes) { String path = ""; try { PostMethod method = new PostMethod(url); method.setRequestHeader("Content-type" , "application/json"); method.setRequestHeader("fileType", fileType); HttpClient httpClient = new HttpClient(); method.setRequestEntity(new ByteArrayRequestEntity(bytes)); int HttpCode = httpClient.executeMethod(method); if (HttpCode != HttpStatus.SC_OK) { throw new IOException("Invalid HttpStatus: " + HttpCode); } path = method.getResponseBodyAsString(); } catch (IOException e) { System.err.println(e.getMessage()); } return path; } }
通常,SpringMVC 中使用 MultipartFile 進行文件上傳,通過這樣的方法獲取到上傳過來文件的byte[]數組
@RequestMapping("/upload") @ResponseBody public String uploadFile(HttpServletRequest request){ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); byte[] bytes = null; try { bytes = file.getBytes(); } catch (IOException e) { e.printStackTrace(); } return null; }
服務器端使用,使用該方法來獲取對應的上傳文件byte[]數組
public String upLoad(@RequestBody byte[] inputData, HttpServletRequest request) { }
網友使用的byte[]數組轉文件的方式,沒有試過,不過這個不是重點,重點在於中間轉發點,只要能拿到byte[]數組,轉文件還是比較容易的
public class T3 { public static void main(String[] args){ String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx"; String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src"; String outFileName = "2.docx"; getFile(getBytes(filePath),outFilePath,outFileName); } /** * 獲得指定文件的byte數組 */ public static byte[] getBytes(String filePath){ byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 根據byte數組,生成文件 */ public static void getFile(byte[] bfile, String filePath,String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在 dir.mkdirs(); } file = new File(filePath+"\\"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }
整體的使用方式已經寫出來了,用在什么地方還需要討論,不過,這樣的實現方式還是很不錯的,減少了臨時文件的存放,而且,代碼量減少很多。