圖片上傳之後臺(java-springboot)實現

前序:服務端使用fastDFS文件管理系統;

1.service服務層實現代碼:

@Service
public class UploadPhotoServiceImpl implements UploadPhotoService {
    private static final String FASTDFS_CLIENT_PROPERTIES = "fdfs_client.properties";

    private static TrackerClient trackerClient = null;
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;
    private static StorageClient storageClient = null;
    private static final String groupName = "group1";


    static {
        // 加載配置文件
        try {
            ClientGlobal.initByProperties(FASTDFS_CLIENT_PROPERTIES);
        } catch (IOException | MyException e) {
            e.printStackTrace();
            System.out.println("load config file fail");
        }
        try {
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("init fail");
        }
    }



    @Override
    public String uploadPhoto(byte[] file_buff, String ext_Name, String file_Name) throws IOException, MyException {
        String filename = uploadFile(file_buff, null,null);
        return filename;
    }

    public String uploadFile(byte[] byteFile, String ext_file, String file_Name) {
        // 拼接服務區的文件路徑
        StringBuffer sbPath = new StringBuffer();
        sbPath.append("http://192.168.0.10:9999/");
        try {
            String[] strings = storageClient.upload_file(byteFile, "png", null);
            System.out.println(strings[0]);
            System.out.println(strings[1]);
            sbPath.append(strings[0]);
            sbPath.append("/"+strings[1]);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return sbPath.toString();
    }

}

2.Controller層實現代碼:

@RestController
@RequestMapping("/upload")
@CrossOrigin//允許跨域請求
public class UploadPhotoController {


    @Autowired
    private UploadPhotoService uploadPhotoService;

    @PostMapping("/picture")
    public ResultVO list(MultipartFile file, HttpServletRequest request) throws ClientException, IOException {
        System.out.println(file);
        if (null == file){
            return  new ResultVO("000001","請上傳圖片",null);
        }
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = null;
        try {
            filename = uploadPhotoService.uploadPhoto(bytes, null, null);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        ResultVO resultVO = new ResultVO("000000","上傳成功",filename);
        return resultVO;
    }
}

配置文件:

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 11110   # tracker Http端口
http.anti_steal_token = no      # 暫無作用
http.secret_key = FastDFS1234567890     # 暫無作用
fastdfs.tracker_servers = 192.168.0.10:22122

 

發佈了106 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章