审查视图

src/main/java/com/tianbo/warehouse/controller/UploadController.java 4.0 KB
朱兆平 authored
1 2 3 4 5 6 7 8 9
package com.tianbo.warehouse.controller;

import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.Attachment;
import com.tianbo.warehouse.service.AttachmentService;
import com.tianbo.warehouse.util.Date.DateUtil;
import com.tianbo.warehouse.util.Helper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Value;
朱兆平 authored
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static com.tianbo.warehouse.util.IO.FileTool.readProperties;

@RestController
public class UploadController {

    @Autowired
    private AttachmentService attachmentService;
27 28
    @Value("${web.upload-path}")
    private String uploadPath; // =./upload/
朱兆平 authored
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    /**
     * 单个文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public ResultJson upload(@RequestParam MultipartFile file) throws Exception {
       ResultJson resultJson =  new ResultJson();
                if (null!=file && !file.isEmpty() && file.getSize()>0){
                    byte[] filebyte = file.getBytes();
                    String fileLitleName = file.getName();
                    String fileName = file.getOriginalFilename();
                    String fileType =  fileName.substring((fileName.lastIndexOf(".")));
                    StringBuffer stringBuffer = new StringBuffer();
44
                    String saveFileName =  stringBuffer.append(uploadPath).append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(fileType).toString();
朱兆平 authored
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
                    File saveFile = new File(saveFileName);
                    if(!saveFile.getParentFile().exists()){
                        saveFile.mkdirs();
                    }

                    try{
                        FileUtils.writeByteArrayToFile(saveFile,filebyte);
                        Attachment attachment = new Attachment(saveFileName);
                        int i =attachmentService.insertSelective(attachment);
                        resultJson = (i==1 ? new ResultJson("200","附件入库成功",saveFileName) :new ResultJson("500","附件入库失败"));
                    }catch (IOException e){
                        resultJson = new ResultJson("400","附件存储失败",e);
                        e.printStackTrace();
                    }catch (IllegalStateException e){
                        resultJson = new ResultJson("400","附件存储失败",e);
                        e.printStackTrace();
                    }
                }
                return resultJson;
    }

    /**
     * 批量上传
     * @param files
     * @return
     * @throws Exception
     */
    @PostMapping("/uploads")
    public ResultJson upload(MultipartFile[] files) throws Exception {
        if (files!=null && files.length>0){
            // ... 处理文件储存逻辑
            for (MultipartFile file : files) {
                if (!file.isEmpty() && file.getSize()>0){
                    String fileName = file.getName();
                    String fileType = file.getContentType();
                    String saveDir = readProperties("uploadDirectory");
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append(saveDir).append("/").append(DateUtil.getToday()).append("/").append(fileType).append(Helper.getUUID()).append(fileName);
                    File saveFile = new File(stringBuffer.toString());
                    try{
                        file.transferTo(saveFile);
                    }catch (IOException e){
                        e.printStackTrace();
                    }catch (IllegalStateException e){
                        e.printStackTrace();
                    }

                }

            }
        }
        return new ResultJson();
    }
}