1. 程式人生 > >MultipartFile實現附件上傳功能

MultipartFile實現附件上傳功能

服務端附件上傳程式碼

package com.oysept.attachment.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件上傳controller
 * @author ouyangjun
 */
@Controller
@RequestMapping(value = "/attachment")
public class UploadController {

	/**
	 * 伺服器附件上傳介面
	 * @param files
	 * @param fileName
	 * @return
	 * 請求地址: http://localhost:8080/attachment/upload/fileUpload
	 */
	@RequestMapping(value = "/upload/fileUpload", method = RequestMethod.POST)
	@ResponseBody
	public String fileUpload(@RequestParam(value="files") MultipartFile files, @RequestParam(value="fileName") String fileName) {
		
		// 附件名稱
		System.out.println("AAAAAAAAAA" + fileName);
		try {
			// 流
			System.out.println("BBBBBBBBBB" + files.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 具體業務邏輯可行編寫
		
		return "SUCCESS";
	}
	
	
}

客戶端附件上傳功能測試方法

package com.oysept.attachment.test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;

/**
 * 附件上傳測試
 * @author ouyangjun
 */
public class UploadTest {

	public static void main(String[] args) {
		
	    try{
	    	CloseableHttpClient httpClient = HttpClients.createDefault();
	    	
	        // 附件上傳的地址
	        HttpPost httpPost =new HttpPost("http://localhost:8080/attachment/upload/fileUpload");
	        
	        // 把流轉換成物件
	        File file =new File("D:\\logo.png");
	        FileBody fileBody =new FileBody(file);
	        
	        StringBody fileName =new StringBody("附件名稱", ContentType.create("text/plain", Consts.UTF_8));
	        
	        // 封裝實體
	        // 設定瀏覽器相容模式,解決檔名亂碼問題(HttpMultipartMode.BROWSER_COMPATIBLE)
	        HttpEntity requestEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
	                .addPart("files", fileBody)//對應伺服器的MultipartFile
	                .addPart("fileName", fileName)// 對應伺服器的String
	                .setCharset(CharsetUtils.get("UTF-8")).build();
	 
	        httpPost.setEntity(requestEntity);
	        
	        // 執行請求
	        CloseableHttpResponse response = httpClient.execute(httpPost);
	        try{
	            // 獲取響應物件
	            HttpEntity responseEntity = response.getEntity();
	            if(responseEntity !=null) {
	                // 內容
	                System.out.println("print----: "+ EntityUtils.toString(responseEntity, Charset.forName("UTF-8")));
	            }
	            // 關閉
	            EntityUtils.consume(responseEntity);
	        } catch (IOException e) {
				e.printStackTrace();
			}
	    } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

本章完結,待續!

本文說明:該文章屬於原創,如需轉載,請標明文章轉載來源