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"));
}
}
文件流保存图片java
最新推荐文章于 2024-07-10 02:47:11 发布