1. 程式人生 > >SpringBoot配置檔案上傳

SpringBoot配置檔案上傳

注意:預設SpringBoot使用的是Spring本身的上傳功能,如需使用apache upload需配置轉換器

1、在properties檔案中配置檔案上傳key-value

#預設支援檔案上傳
spring.http.multipart.enabled =true
spring.http.multipart.file-size-threshold =0
# 上傳檔案的臨時目錄
#spring.http.multipart.location=E:/upload/temp/
# 最大支援檔案大小
spring.http.multipart.max-file-size =100MB
# 最大支援請求大小
spring.http.multipart.max-request-size =100Mb

2、上傳檔案程式碼

@RequestParam ("file" ) file為頁面提交過來的欄位名,如:<input type="file" name="file"> 對應name值

@Slf4j
@Controller
@RequestMapping("/func" )
public class FunctionController extends BaseController {
     @Autowired
     private UploadConfig uploadConfig;

     @RequestMapping(value = "/uploadFileLocal", method = RequestMethod.POST )
     public void uploadFileLocal(@RequestParam ("file" ) MultipartFile file, HttpServletRequest request , HttpServletResponse response ) throws Exception {
         JSONObject json = new JSONObject();
         
         String innerCode = request.getParameter("innerCode" );
          log.info("開始接收機器innerCode-{}的檔案" , innerCode);
         
          if(StringUtils.isEmpty( innerCode)) {
              json.put("code" , 500);
              json.put("msg" , "機器號innerCode不能為空" );
             writeJSON( json, response );
              return ;
         }
         
          if(file .isEmpty()) {
              json.put("code" , 500);
              json.put("msg" , "沒有上傳的檔案流" );
             writeJSON( json, response );
              return ;
         }
         
         String fileName = file.getOriginalFilename();
         String path = uploadConfig .getReceiveRoot() + "/" + innerCode + "/";
          log.info("接收檔案儲存路徑-{},檔案大小為-{}" , path , file .getSize());
         File localFile = new File(path);
         
          if(!localFile .exists()) {
              localFile.mkdirs();
         }
        try {
         
          file.transferTo(new File(path+fileName));
           
          //downloadUrl為包的下載目錄
         String downloadUrl = uploadConfig.getReceiveDownloadRoot() + innerCode + "/" + fileName ;
          json.put("code" , 200);
              json.put("url" , downloadUrl );
          log.info("檔案{}上傳成功" , downloadUrl );
         } catch (Exception e ) {
              json.put("code" , 500);
              json.put("msg" , "上傳發生異常" );
              log.error("上傳檔案發生異常,異常資訊:" , e );
         }
        writeJSON( json, response );
    }
}

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

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Configuration
public class UploadConfig {
    
    //接收檔案服務URL
    @Value("${upload.RECEIVE_FILE_URL}")
    private String receiveFileUrl;
    
    //接收檔案目錄
    @Value("${upload.RECEIVE_ROOT}")
    private String receiveRoot;
    
    //接收檔案下載根域名地址
    @Value("${upload.RECEIVE_DOWNLOAD_ROOT}")
    private String receiveDownloadRoot;
    
    //上傳根目錄
    @Value("${upload.UPLOAD_ROOT}")
    private String uploadRoot;

    //下載根域名地址
    @Value("${upload.DOWNLOAD_ROOT}")
    private String downloadRoot;
    
    //檔案字尾
    @Value("${upload.SUFFIXLIST}")
    private String suffixList;
    
    public String getParentPath( int index ) {
          if(index == 1) {
              return "/uboxsys" ;
         } else if (index == 2) {
              return "/lua" ;
         } else if (index == 3) {
              return "/web" ;
         } else if (index == 4) {
              return "/script" ;
         }
          return null ;
    }
}