public class ImageUrlUtils {
public static String getImageUrlByInputStream(ServletInputStream inputStream) throws IOException{
String urlFilePath = new String();
urlFilePath = "uploadpic/";
File direct = new File(filePath);
String lstr = Long.toString(System.currentTimeMillis());
/*判断图片类型 start*/
int position=-1;
byte[] buffer = new byte[1024];
position=inputStream.readLine(buffer, 0, buffer.length);
String fileType="";
if(position!=-1){
position = inputStream.readLine(buffer, 0, buffer.length);
if(position!=-1){
String upFileName = new String(buffer,0,position);
int fileNamePosition = 0;
if((fileNamePosition=upFileName.indexOf("."))!=-1){
fileType=upFileName.substring(fileNamePosition).split("\"")[0];
}
}
}
/*判断图片类型 end*/
String fileName = lstr.substring(7)+fileType;
if (!direct.exists()) {
direct.mkdirs();
}
if (!direct.exists()) {
direct.mkdirs();
}
BufferedImage chooseimage = ImageUtils
.getImage(inputStream);
FileOutputStream fos = new FileOutputStream(filePath+ "/" + fileName);
ImageIO.write(chooseimage, "jpg", fos);
fos.close();
chooseimage.flush();
inputStream.close();
return "图片url";
}
}
图片上传的一个类 分享给大家
最新推荐文章于 2020-02-15 13:08:54 发布