1.网络文件下载

 @GetMapping("downgd")@ResponseBodypublic Result downloadNet(HttpServletResponse response,String path) throws MalformedURLException {// 下载网络文件 Result result = new Result();         URL url = new URL(path);    try {        UUID uuid = UUID.randomUUID();        URLConnection conn = url.openConnection();                 InputStream in = conn.getInputStream();        OutputStream out = response.getOutputStream();        response.setHeader("content-disposition","        attachment;        filename=" +URLEncoder.encode(uuid+".jpg", "UTF-8"));        byte [] by = new byte[1024];        int i = 0;        while((i=in.read(by))!=-1)        {            out.write(by,0,i);//通过浏览器下载        } //下载到具体门路       //FileOutputStream fs = //      new FileOutputStream("e:/"+uuid+".jpg");//byte[] buffer = new byte[1204];//int length;//while ((byteread = inStream.read(buffer)) != -1) {//        bytesum += byteread;//        fs.write(buffer, 0, byteread);//}                in.close();    }catch (Exception e){        e.printStackTrace();        result.setCode(Result.Code.SYSTEMERROR);    }    return result;}

2.图片合成增加文字

1、BatchNumberUtils工具类

package com.cy.file_onload.qrCode;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;import java.util.concurrent.atomic.AtomicInteger;public class BatchNumberUtils {    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");    private static final AtomicInteger atomicInteger = new AtomicInteger(1000000);//  创立不间断的订单号 //@param no 数据中心编号//  @return 惟一的、不间断订单号public static synchronized String getOrderNoByUUID(String no) {        Integer uuidHashCode = UUID.randomUUID().toString().hashCode();        if (uuidHashCode < 0) {            uuidHashCode = uuidHashCode * (-1);        }        String date = simpleDateFormat.format(new Date());        return no + date + uuidHashCode;    } // 获取同一秒钟 生成的订单号间断  //@param no 数据中心编号 // @return 同一秒内订单间断的编号public static synchronized String getOrderNoByAtomic(String no) {        atomicInteger.getAndIncrement();        int i = atomicInteger.get();        String date = simpleDateFormat.format(new Date());        return no + date + i;    }     // 获取以后日期组成的文件名  //@param name 文件名前缀 // @return 组成的文件名public static synchronized String getFileNameByDate(String name) {        String date = dateFormat.format(new Date());        return name +"/"+ date;     }  }

2、图片合成全代码

@Controller@RequestMapping("/test/")public class ResumeTemplateServiceImpl {private static String ComposeFileNameManual = "compose_images";  //申请 url 中的资源映射,不举荐写死在代码中, // 最好提供可配置,如 upload_flowChart  private String resourceHandler="/upload_flowChart/";  //上传文件保留的本地目录,应用@Value获取全局配置 //文件中配置的属性值,如 E:/java/upload_flowChart  private String uploadImagesLocation = "E://";  // 合成图片 * @param templatePath 模板地址 // @param seedPath 二维码地址 // @return种子模板链接  @RequestMapping("imgAdd")public String composePic(String templatePath, String seedPath) {        templatePath = "背景图片门路";        seedPath = "指标图片门路";try {//文件名 String picName = UUID.randomUUID().toString() + ".jpg";//日期格局文件夹 String composeFileName = BatchNumberUtils.getFileNameByDate(ComposeFileNameManual);//合成图片文件夹 File pathFile = new File(uploadImagesLocation + File.separator + composeFileName);//合成文件门路 String path = uploadImagesLocation + File.separator + composeFileName + File.separator + picName;//数据库贮存地址 String dataPath = resourceHandler.substring(0, resourceHandler.length() - 2) + composeFileName + "/" + picName;     if (seedPath == null) {        File file = new File(seedPath);          if (!file.isFile()) {            System.out.println("图片源门路不是一个文件");        }           System.out.println("图片源门路不存在");      }         if (templatePath == null) {          File file = new File(templatePath);            if (!file.isFile()) {            System.out.println("背景图片门路不是一个文件");             }               System.out.println("背景图片门路不存在");            }            if (!pathFile.exists()) {                pathFile.mkdirs();            }//---------------------------------合成图片步骤----------------------------- //背景 File templateFlie = new File(templatePath);            BufferedImage bg = ImageIO.read(templateFlie);//读取背景图片 int height = bg.getHeight();//背景图片的高 int width = bg.getWidth();  //背景图片的宽 BufferedImage qcCode = ImageIO.read(new File(seedPath));  //读取二维码图片 300 * 300 BufferedImage img = new BufferedImage(184, 312, BufferedImage.TYPE_INT_RGB);//创立画布 Graphics g = img.getGraphics();//生成画笔 开启画图 // 绘制背景图片 g.drawImage(bg.getScaledInstance(184, 312, Image.SCALE_DEFAULT), 0, 0, null); // 绘制放大后的图 //绘制二维码图片  定位到背景图的右下角 /*    g.drawImage(qcCode.getScaledInstance(width / 4, width / 4, Image.SCALE_DEFAULT), width - (width / 4)-10, height - (width / 4)- 10, null); // 绘制放大后的图*/ //绘制二维码图片,居中 g.drawImage(qcCode.getScaledInstance(180, 180, Image.SCALE_DEFAULT),                    1, height/23, null); // 绘制放大后的图 Font font = new Font("微软雅黑", Font.PLAIN, 15);// 增加字体的属性设置 g.setFont(font);            g.setColor(Color.BLACK);// 截取用户名称的最初一个字符 String lastChar = "张三".substring("张三".length() - 1);// 拼接新的用户名称 String newUserName = "张三".substring(0, 1) + "**" + lastChar + " 的二维码";// 增加用户名称 g.drawString(newUserName, 2, 270);//关掉画笔 g.dispose();ImageIO.write(img, "jpg", new File(path));System.out.println("合成图片胜利,门路:" + path);File file = new File(path);System.out.println(file);//返回合成图片的门路 return dataPath; } catch (Exception e) {return e.getMessage();//throw new CustomException("图片合成失败", 400); }    }}