Upload multiple picture files and pass PostMan test

Keywords: Java

The framework of the project is a service of springCloud. Image upload is actually to read and write files with java IO stream, passing a path to the background. The background links to the front-end through the browser, and copies the front-end image data to the storage address of the server.

1. Address of the server to save the picture:

Add the following configuration to the application.yml file:

img:
  location: E:\hjy\workspace\wx-back-web\src\main\webapp\olimg\

2. Implementation of file upload

Get path in. Controller

	import org.springframework.beans.factory.annotation.Value;


  @Value("${img.location}")
	private String location;

Method in Controller:

import org.springframework.web.bind.annotation.PathVariable;


@RequestMapping("/uploadImg/{imgType}")
	public String uploadImg(
			@RequestParam("ediormd-image-file") MultipartFile[] files,@PathVariable("imgType") String imgType) {
		ResponseData res = new ResponseData();
         List<ProductPic> proPicList = new ArrayList<ProductPic> ();
        String filePath = location + imgType +"/";
        try {
        	if(files != null && files.length >0){
        		for( int i=0;i<files.length;i++){
        			ProductPic proPic = new ProductPic();
        			MultipartFile file = files[i];
        			String contentType = file.getContentType();
        			String fileName = file.getOriginalFilename();
        			System.out.println("fileName-->" + fileName);
        			System.out.println("getContentType-->" + contentType);
        			String resfileNewName = FileUtil.uploadFile(file.getBytes(), filePath, fileName);
        			proPic.setPicUrl(filePath +resfileNewName);
        			if(imgType.equals("list")){
        				proPic.setPicName("List diagram");
        			}else if("caro".equals(imgType)){
        				proPic.setPicName("Rotation chart");
        			}else if("intr".equals(imgType)){
        				proPic.setPicName("Introduction diagram");
        			}else if("thum".equals(imgType)){
        				proPic.setPicName("thumbnail");
        			}
        			proPicList.add(proPic);
        		}
        	}
			res.setCode(Constant.SUCCESS_CODE);
			res.setResult(proPicList);
			res.setMessage(Constant.SUCCESS_MSG);
		} catch (IOException e) {
			e.printStackTrace();
			res.setCode(Constant.FAIL_CODE);
			res.setMessage(Constant.FAIL_MSG);
		} catch (Exception e) {
			e.printStackTrace();
			res.setCode(Constant.FAIL_CODE);
			res.setMessage(Constant.FAIL_MSG);
		}
		JSONObject rdJson = JSONObject.fromObject(res);
		return rdJson.toString();
	}

Secondary class ResponseData

public class ResponseData {
	private Object result; // Return result
	private Object baseResult;// Basic information return result
	private String code; // Return to code
	private String message; // Return to reminder message

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public Object getBaseResult() {
		return baseResult;
	}

	public void setBaseResult(Object baseResult) {
		this.baseResult = baseResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

File upload service class:

public class FileUtil {

	/**
	 * Upload the file and return the name of the file
	 * @param file
	 * @param filePath
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String uploadFile(byte[] file,String filePath,String fileName) throws Exception{
		String time = DateHelper.formatDate(new Date(), "yyMMddHHssmm");
        fileName = time +"_" +fileName ;
		File targetFile = new File(filePath);
		if(!targetFile.exists()){
			targetFile.mkdir();
		}
		
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
		
		return fileName;
	}
}

3. Pass PostMan test

Install the one marked in red as shown in the figure below to test the image upload,

Posted by kiwis on Tue, 24 Dec 2019 07:01:55 -0800