图片上传之后台(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

 

发布了124 篇原创文章 · 获赞 11 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览