FastDFSClient.java 8.94 KB
package com.lhcredit.framework.web.service;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.service.TrackerClient;
import com.lhcredit.common.utils.FileSysUtils;
import com.lhcredit.project.business.bCreditReport.domain.BCreditReport;
import com.lhcredit.project.business.bCreditReport.service.IBCreditReportService;
import com.lhcredit.project.business.bReportTable.service.IBReportTableService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
@RestController
@Component
@RequestMapping("web/test/")
public class FastDFSClient {

    private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private IBCreditReportService bCreditReportService;
    @Autowired
    private IBReportTableService tableService;

//    @RequestMapping("aaaa")
    public void test() throws IOException {
        List<BCreditReport> bCreditReports = bCreditReportService.selectBCreditReportList(null);
        bCreditReports=bCreditReports.stream().sorted(Comparator.comparing(BCreditReport::getCreateTime)).collect(Collectors.toList());
        List<String> list=new ArrayList<>();
        for (BCreditReport bCreditReport : bCreditReports) {
            System.out.println(bCreditReport.getId());
            try{
                //跳出生成doc 为null 的
                if (StringUtils.isEmpty(bCreditReport.getFirstUrl())) continue;
                String doc=null;
                if (StringUtils.isNotEmpty(bCreditReport.getFirstUrl())){
                    JSONArray arr = JSONArray.parseArray(bCreditReport.getFirstUrl());
                    JSONObject jsonObject = arr.getJSONObject(0);
                    String fileAttachments=jsonObject.getString("fileAttachments");
                    String s = uploadFile(fileAttachments);
                    jsonObject.put("fileAttachments",s);
                    doc=s;
                    arr.set(0,jsonObject);
                    bCreditReport.setFirstUrl(arr.toJSONString());
                }
                if (StringUtils.isNotEmpty(bCreditReport.getFinalUrl())){
                    JSONArray arr = JSONArray.parseArray(bCreditReport.getFinalUrl());
                    JSONObject jsonObject = arr.getJSONObject(0);
                    String fileAttachments=jsonObject.getString("fileAttachments");
                    String s = uploadFile(fileAttachments);
                    jsonObject.put("fileAttachments",s);
                    arr.set(0,jsonObject);
                    bCreditReport.setFinalUrl(arr.toJSONString());
                }
                if (StringUtils.isNotEmpty(bCreditReport.getPdfUrl())){
                    JSONArray arr = JSONArray.parseArray(bCreditReport.getPdfUrl());
                    JSONObject jsonObject = arr.getJSONObject(0);
                    String fileAttachments=jsonObject.getString("fileAttachments");
                    String s = uploadFile(fileAttachments);
                    jsonObject.put("fileAttachments",s);
                    arr.set(0,jsonObject);
                    bCreditReport.setPdfUrl(arr.toJSONString());
                    bCreditReport.setUrl(arr.toJSONString());
                }
                if (StringUtils.isNotEmpty(bCreditReport.getAnnex())&&bCreditReport.getAnnex().contains("{\"doc\"")){
                    JSONObject jsonObject = JSONObject.parseObject(bCreditReport.getAnnex());
                    JSONObject docF = jsonObject.getJSONObject("doc");
                    JSONObject sfExcel = jsonObject.getJSONObject("sfExcel");
                    JSONObject scoreCard = jsonObject.getJSONObject("scoreCard");
                    docF.put("fileAttachments",doc);jsonObject.put("doc",docF);
                    if (null!=sfExcel){
                        String s = uploadFile(sfExcel.getString("fileAttachments"));
                        sfExcel.put("fileAttachments",s);jsonObject.put("sfExcel",sfExcel);
                    }
                    if (null!=scoreCard){
                        String s = uploadFile(scoreCard.getString("fileAttachments"));
                        scoreCard.put("fileAttachments",s);jsonObject.put("scoreCard",scoreCard);
                    }
                    bCreditReport.setAnnex(jsonObject.toJSONString());
                }
                bCreditReportService.updateBCreditReport(bCreditReport);
            }catch (Exception e){
                e.printStackTrace();
                list.add(bCreditReport.getId());
            }
        }

    }
    public String uploadFile(String fileUrl) throws IOException {
        if (fileUrl.contains("fileStorage")){
            throw new RuntimeException("文件已上传fileStorage");
        }
        if (fileUrl.contains("img30.lhdna.com")){
            fileUrl=fileUrl.replace("img30.lhdna.com","file2.lhdna.com");
        }
        InputStream inputStream = HttpRequest.get(fileUrl).execute().bodyStream();

        String s = FileSysUtils.upLoadFile(inputStream, fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.length()), FileSysUtils.DirectoryType.creditManager.directory);
        return FileSysUtils.getFullPath(s);
    }
    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public synchronized String uploadFile(MultipartFile file) throws IOException {
        String path = FileSysUtils.getFullPath(FileSysUtils.upLoad(file));
        return path;
//        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
//        return getResAccessUrl(storePath);
    }
    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFileORI(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return storePath.getFullPath();
    }
    /**
     * 上传头像
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadAvatar(MultipartFile file) throws IOException {
//        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), "png",null);
//        return storePath.getFullPath();
        String path = FileSysUtils.getFullPath(FileSysUtils.upLoad(file));
        return path;
    }

    /**
     * 上传微信头像
     * @param urlStr 文件路径
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadWx(String urlStr) throws IOException {

            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        StorePath storePath = storageClient.uploadFile(conn.getInputStream(),conn.getContentLength(), "jpeg",null);
        return storePath.getFullPath();
    }

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public synchronized String uploadFile(File file)   {
        String path = FileSysUtils.getFullPath(FileSysUtils.upLoadFile(file));
        return path;
    }



    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fullpath=storePath.getFullPath();
        String suffix = fullpath.substring(fullpath.indexOf("/"));
        String fileUrl = fdfsWebServer.getWebServerUrl() + suffix;
        return fileUrl;
    }

    /**
     * 下载文件
     * @param fileUrl 文件url
     * @return
     */
    public byte[]  download(String fileUrl) {
        return HttpRequest.post(fileUrl).execute().bodyBytes();
    }
    //全路径下载
    public byte[]  download2(String fileUrl) {
        return HttpRequest.post(fileUrl).execute().bodyBytes();
    }


}