FileControl.java 1.66 KB
package com.example.dahua.control;

import com.example.dahua.bean.UploadImg;
import com.example.dahua.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
@Api("文件管理器")
@RequestMapping("file/*")
public class FileControl {

    @Autowired
    UserService userService;

    @RequestMapping(method = RequestMethod.POST, value = "uploadImg")
    public String uploadImg(@RequestParam("file") MultipartFile file, @RequestParam("schoolId") String schoolId, @RequestParam("studentCode") String studentCode,
                            @RequestParam("clint_type") String clint_type,@RequestParam("userType") int userType) {
        System.out.println("schoolId:" + schoolId + " studentCode:" + studentCode);
        String fileName = file.getOriginalFilename();//文件名

        File outFile = new File("C://imgCom");
        if (!outFile.exists()) outFile.mkdirs();
        try {
            File dest = new File(outFile, fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(dest);

            fileOutputStream.write(file.getBytes());
            userService.uploadImgAndUserInfo(dest.getAbsolutePath(), schoolId, studentCode, clint_type,userType);
            return "1";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }
}