springbootファイルをMultiiprtFile fileにアップロードします.
1、springbootファイルを説明するMultiiprtFile fileをアップロードします.Spring MVCから来ました. 注意点: htmlページに直接アクセスするには、springbootデフォルトで読み込まれているフォルダの下にhtmlを置く必要があります. MultiiprtFileオブジェクトのtransferToメソッドは、ファイルの保存に使用されます(効率と操作は、当初はFileOutStreamよりも便利で効率的です).
直接コード
はcontrollerコードです.demoだけです. の ロジックコードはserver に いてあります.
直接コード
uploadimg.html
はcontrollerコードです.demoだけです. の ロジックコードはserver に いてあります.
package com.study.studyBoot.controller;
import com.study.studyBoot.model.JsonData;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class FileController {
//final Logger logger = logger.getLogger(FileController.class);
private static final String filePath = "D:/IDEA/studyBoot/src/main/resources/static/images/";
/**
* @Description:
* @author: panboyang
* @date :2019-06-26 17:55:17
* @params: head_img ,filePath
*/
@RequestMapping(value = "upload")
@ResponseBody
public JsonData upload(@RequestParam("head_img") MultipartFile multipartFile, HttpServletRequest request) {
//multipartFile.getSize();
JsonData data = new JsonData();
if (multipartFile.isEmpty()) {
return new JsonData(-1, " ");
} else {//
String name = request.getParameter("name");
System.out.println(" :" + name);
//
String fileName = multipartFile.getOriginalFilename();
System.out.println(" :" + fileName);
// , jpeg,png
String suffixName = fileName.substring(fileName.lastIndexOf("."));
System.out.println(" :" + suffixName);
//
fileName = UUID.randomUUID() + suffixName;
System.out.println(" :" + fileName);
File dest = new File(filePath + fileName);
//dest.getPath()
try {
multipartFile.transferTo(dest);
return new JsonData(1,dest.getPath(),"sues to save");
}catch (IllegalStateException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return new JsonData(-1,null,"fail to save");
}
}
}