java ftpService和另外一篇java处理富文本结合使用

直接上代码:

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sunwin.yog.comm.FileHelper;
import sunwin.yog.comm.FtpUtil;
import sunwin.yog.comm.S;
import sunwin.yog.comm.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class FtpService {
    /*private static String ftpHost="192.168.20.99";
    private static String ftpUserName="wang";
    private static String ftpPassword="123456";
    private static int ftpPort=21;*/

    /**
     * 文件上传
     * @param ftpPath
     * @param fileName 为空采用原文件名
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadFile(String ftpPath,String fileName,MultipartFile file) throws Exception{
        InputStream inputStream=file.getInputStream();
        if(StringUtil.isEmpty(fileName)){
            fileName=file.getOriginalFilename();
        }
        boolean flag=FtpUtil.uploadFile(S.ftpHost,S.ftpUserName,S.ftpPassword,S.ftpPort,ftpPath,fileName,inputStream);
        if(flag){
            return ftpPath+"/"+fileName;
        }else{
            return null;
        }


    }

    /**
     * 自动生成文件名
     * @param ftpPath
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadFile(String ftpPath,MultipartFile file) throws Exception{
        FileHelper fileHelper=new FileHelper();
        String extname=fileHelper.getExtensionName(file.getOriginalFilename());//获取后缀
        String fileName= UUID.randomUUID().toString()+"."+extname;
        return uploadFile(ftpPath,fileName,file);
    }

    /**
     * 上传多个文件
     * @param ftpPath
     * @param files
     * @return
     * @throws Exception
     */
    public List<String> uploadMulFile(String ftpPath,MultipartFile[] files) throws Exception{
        List<String> fileNames=new ArrayList<>();
        for(MultipartFile file:files){
            fileNames.add(uploadFile(ftpPath,file));

        }
        return fileNames;
    }
    /**
     * 删除文件
     * @param ftpUrl 文件路径
     * @return
     * @throws Exception
     */
    public boolean deleteFile(String ftpUrl) throws Exception{
        if(StringUtil.isNotEmpty(ftpUrl)){
            String ftpPath=null;
            String ftpName=null;
            if(ftpUrl.contains("/")){
                ftpPath=ftpUrl.substring(0,ftpUrl.lastIndexOf("/"));
                ftpName=ftpUrl.substring(ftpUrl.lastIndexOf("/")+1,ftpUrl.length());
            }else{
                ftpPath="/";
                ftpName=ftpUrl;
            }

            return FtpUtil.deleteFile(S.ftpHost,S.ftpUserName,S.ftpPassword,S.ftpPort,ftpPath,ftpName);
        }else{
            return false;
        }

    }

    /**
     * 删除多个文件
     * @param ftpUrls
     * @throws Exception
     */
    public void deleteMulFile(List<String> ftpUrls) throws Exception{
        if(ftpUrls!=null&&ftpUrls.size()>0){
            for(String ftpUrl:ftpUrls){
                deleteFile(ftpUrl);
            }
        }
    }

    /**
     * 添加base64图片
     * 处理含有base64位格式的图片,保存为图片并替代为地址
     * @param content
     */
    public String handlerBase64Content(HttpServletRequest request,String parentName,String content) throws Exception{
        if(StringUtil.isNotEmpty(content)){
            //获取src值
            List<String> srcvalues=StringUtil.getImgSrc(content);
            String temp=null;
            for(String src:srcvalues){
                if(src.startsWith("data:image/")){
                    temp=uploadBase64File(request,src,parentName);
                    content=content.replace(src,"SWphotoUrl"+temp);
                }else if(src.contains(parentName)){
                    temp=src.substring(src.indexOf(parentName));
                    content=content.replace(src,"SWphotoUrl"+temp);
                }else{
                    //其他的属于网络图片,不用处理
                }

            }
        }
        return content;
    }

    /**
     * 删除图片
     * 删除含有img的内容的图片
     * @param content
     * @throws Exception
     */
    public void delBase64File(String content) throws Exception{
        //获取src值
        List<String> srcvalues=StringUtil.getImgSrc(content);
        String temp=null;
        for(String src:srcvalues){
            temp=src.replace("SWphotoUrl","");
            deleteFile(temp);
        }
    }
    /**
     * 上传base64位的图片
     * @param request
     * @param imgStr
     * @param parentName
     * @throws Exception
     */
    private String uploadBase64File(HttpServletRequest request,String imgStr, String parentName) throws Exception{
        String path = request.getSession().getServletContext().getRealPath("")+"/";
        String photoName = GenerateImage(request, imgStr, parentName);
        InputStream is=new FileInputStream(path+photoName);
        String fileName=uploadFile(parentName,is);
        File file=new File(path+photoName);
        if(file.exists()) file.delete();
        return fileName;
    }
    /**
     * 上传文件
     * @param ftpPath
     * @param file
     * @return
     * @throws Exception
     */
    private String uploadFile(String ftpPath,InputStream file) throws Exception{
        String fileName=UUID.randomUUID().toString()+".jpg";
        boolean flag= FtpUtil.uploadFile(S.ftpHost,S.ftpUserName,S.ftpPassword,S.ftpPort,ftpPath,fileName,file);
        if(flag){
            return ftpPath+"/"+fileName;
        }else{
            return null;
        }


    }
    //base64字符串转化成图片
    private String GenerateImage(HttpServletRequest request, String imgStr, String parentName) throws Exception{
        FileHelper fileHelper=new FileHelper();
        //对字节数组字符串进行Base64解码并生成图片
        String imagePath = request.getServletContext().getRealPath("photo")+"/"+parentName;
        fileHelper.mkdir(imagePath);
        //String newfilename=System.currentTimeMillis()+"";
        String newfilename=UUID.randomUUID().toString()+"";
        String filePath=imagePath+"/"+newfilename+".jpg";
        BASE64Decoder decoder = new BASE64Decoder();
        //Base64解码
        imgStr=imgStr.substring(imgStr.indexOf("base64,")+7);
        byte[] b = decoder.decodeBuffer(imgStr);
        for(int i=0;i<b.length;++i)
        {
            if(b[i]<0)
            {//调整异常数据
                b[i]+=256;
            }
        }
        //生成jpeg图片
        fileHelper.createNewFile(filePath);
        OutputStream out = new FileOutputStream(filePath);
        out.write(b);
        out.flush();
        out.close();
        imgStr=null;
        return "photo/"+parentName+"/"+newfilename+".jpg";
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值