网络图片地址下载后打码并上传到oss

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.PutObjectRequest;

public class PicEdit {
    
    private static ThreadLocal<FTPClient> ftpClient = new ThreadLocal<FTPClient>();
    // 水印透明度
    private static float alpha = 0.5f;
    // 水印文字大小
    public static final int FONT_SIZE2 = 28;
    // 水印文字字体
    private static Font font = new Font ("微软雅黑", Font.BOLD, FONT_SIZE2);
    private static Logger log=Logger.getLogger(PicEdit.class);
    private String ip;
    private int port;
    private String username;
    private String password;
    private int timeout;
    private static final String FONT_PATH_WEIRUAN = "/msyh.ttf";
    public static String WEB_CONTEXT_PATH;
    private static String bucketName = "yourbucket";
    private static String endPoint = "yourendpoint";
    private static String accessKeyId = "youraccesskeyid";
    private static String accessKeySecret = "youraccessKeySecret";
    
    public static void main(String[] args) throws Exception {
        File[] files = changeFile("https://profile-avatar.csdnimg.cn/e69472df813b4464a708de0faa6a278c_qq_39768404.jpg");
        String[] filesFileName = {"e69472df813b4464a708de0faa6a278c_qq_39768404.jpg"};
        String[] path = getUploadPath();
        String icon = "仅限查看使用";
        upLoadProceduresImage(files,filesFileName,path,icon);
    }
    /**
     * 网络图片转file文件
     * @param pictureUrl网络图片路径
     * @return
     */
    public static File[] changeFile(String pictureUrl) {
        //初始化图片数组
        File[] pic = new File[1];
        //初始化图片名称数组
        String[] picFileName = new String[1];
        try {
            File file = null;
            //获取文件名
            picFileName[0] = pictureUrl.substring(pictureUrl.lastIndexOf("/")+1,pictureUrl.length());
            file = File.createTempFile("temp_image", picFileName[0]);
            URL urlfile = new URL(pictureUrl);
            InputStream inStream = urlfile.openStream();
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            pic[0] = file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pic;
    }
    // 获取上传路径 非原始图路径
    private static String getPathStr(String[] path) {
        StringBuffer pathStr = new StringBuffer();
        for (int i = 0; i < path.length; i++) {
            pathStr.append(path[i]).append("/");
        }
        return pathStr.toString();
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值