文件流保存图片java

package com.example.demo.utils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoadImage {

    public static   String  name(){
        Date date = new Date();
        String time = getFisrtDayOfMonth(date);
        String idsts = getIdstrs(time);
        char c1 = (char) (int) (Math.random() * 26 + 97);
        char c2 = (char) (int) (Math.random() * 26 + 97);
        char c3 = (char) (int) (Math.random() * 26 + 97);
        char c4 = (char) (int) (Math.random() * 26 + 97);
        char c5 = (char) (int) (Math.random() * 26 + 97);
        char c6 = (char) (int) (Math.random() * 26 + 97);
        String questionbankid = idsts + c1+c2+c3+c4+c5+c6;
        return questionbankid;
    }


    //根据时间Date获取字符串String格式的数据
    public static String getFisrtDayOfMonth(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String firstDayOfMonth = sdf.format(date.getTime());
        return firstDayOfMonth;
    }

    //根据String字符串yyyy-MM-dd HH:mm:ss格式数据,拼接成数字字符串
    public static String getIdstrs(String time){
        String result = "";
        String[] two = time.split(" ");
        String[] one = two[0].split("-");
        String[] three = two[1].split(":");
        result+=one[0]+one[1]+one[2]+three[0]+three[1]+three[2]+"";
        return result;
    }

    public static void mkdirs(String address) {
        File file=new File(address);
        if (file.exists() && file.isDirectory()) {
            return;
        }
        if (file.exists()) {
            file.delete();
            file.mkdirs();
        } else {
            file.mkdirs();
        }
    }






    public static String loadpicture(String pic,String tel,String titleid) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(pic));  //原先图片所在路径

     String ad="src\\main\\resources\\static\\image\\"+tel+"\\"+titleid+"\\";//这个根据你本地的路径设置保存点
        mkdirs(ad);
        String picture=name()+".jpg";
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ad+picture));  //你要保存在哪个目录下面
        int i;

        while((i=in.read())!=-1){
            out.write(i);
        }
        out.flush();
        out.close();
        in.close();
        return picture;
    }

public static void main(String args[]) throws IOException {
//需要的图片地址,,,,电话,,,,题号
        System.out.println(loadpicture("C:\\Users\\Administrator\\Desktop\\mysql\\3556745_092302332151_2.jpg",
                "17859901738","4348u3u3"));

}




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭九钦vlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值