这是⼀个Javaweb上传图⽚的⽅法(纯代码)
上传图⽚的⽅法
@ResponseBody
@RequestMapping(value="/upload_images_rich/{fileKind}")
getsavefilenamepublic Object upload_images_rich(@RequestParam MultipartFile file,@PathVariable("fileKind") String fileKind) {
JsonResult<Map<String,Object>> result = new JsonResult<Map<String,Object>>().successMsg();
try{
UploaderFileUtil fileUtil = new UploaderFileUtil(file);
//保存原图缩略图浏览图
UploaderFileBean uploaderFileBean = fileUtil.saveImageOriginal("images", fileKind);
String thumbURL = fileUtil.OriginalURL(),"images", FileName()); String browseURL = fileUtil.OriginalURL(),"images", FileName()); Map<String,Object> map = new HashMap<>();
map.put("src",Const.digitPath + "/" + browseURL);//此处必须给定绝对路径
result.setCode(0);
result.setData(map);
return result;
}catch (Exception e){
Msg();
}
}
UploaderFileUtil⼯具类
package com.wonders.framework.util;
import com.drew.imaging.jpeg.JpegMetadataReader;
import adata.Directory;
import adata.Metadata;
import adata.Tag;
import if.ExifIFD0Directory;
import if.ExifSubIFDDirectory;
import com.dec.jpeg.JPEGCodec;
import com.dec.jpeg.JPEGImageEncoder;
import com.wonders.framework.util.bean.ImageFileBean;
import com.wonders.framework.util.bean.UploaderFileBean;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/
**
* Created by IntelliJ IDEA.
* User:ZJX
* Date:2020/01/09
* Time:15:53
* To change this template use File|Settings|File Templates.
* tif 需要三个包 jai_core.jar jai_imageio-1.1.jar jai_codec.jar
**/
public class UploaderFileUtil {
private MultipartFile mfile;
public UploaderFileUtil() {
}
}
public UploaderFileUtil(MultipartFile mfile) {
this.mfile = mfile;
}
/**
* 保存图⽚为缩率图
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @param fileSuffix ⽂件后缀根据⽂件后缀,判断不同的⽂件处理 tif 和 tiff 为tif处理⽅式(tif会压缩为 jpg 的图⽚)或其他的图⽚格式。 * @return ⽣成缩率图的存储路径
*/
public String saveImageThumb(String originalURL, String storePath, String myFolderName, String originName, String fileSuffix) {
fileSuffix = LowerCase();
if ("tif".equals(fileSuffix) || "tiff".equals(fileSuffix)) {
return this.saveTifToJpgThumb(originalURL, storePath, myFolderName, originName);
} else {
return this.saveImageThumb(originalURL, storePath, myFolderName, originName);
}
}
/**
* 普通图⽚处理为缩率图
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @return ⽣成缩率图的存储路径
*/
public String saveImageThumb(String originalURL, String storePath, String myFolderName, String originName) {
String thumbURL = "default_image.png";
try {
BufferedImage originalImage = ad(new File(Const.uploadFilePath + "/" + originalURL));
thumbURL = ateImageThumb(storePath, myFolderName, originName, originalImage);
} catch (IOException e) {
e.printStackTrace();
}
return thumbURL;
}
/**
* tif 图⽚处理为缩率图 jpg 格式。
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @return ⽣成缩率图的存储路径
*/
public String saveTifToJpgThumb(String originalURL, String storePath, String myFolderName, String originName) {
originName = originName.substring(0, originName.lastIndexOf(".")) + ".jpg";
String thumbURL = "default_image.png";
ImageReader reader = null;
FileImageInputStream inputStream = null;
try {
reader = ImageReadersByFormatName("tiff").next();
inputStream = new FileImageInputStream(new File(Const.uploadFilePath + "/" + originalURL));
reader.setInput(inputStream);
BufferedImage originalImage = ad(0);
thumbURL = ateImageThumb(storePath, myFolderName, originName, originalImage);
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
return thumbURL;
}
/**
* 创建缩率图
*/
private String createImageThumb(String storePath, String myFolderName, String originName, BufferedImage originalImage) throws IOException { String save_path = Const.uploadFilePath + "\\" + storePath + "\\" + Const.folder_thumb + "\\" + myFolderName;
File save_file_url = new File(save_path);
//⽂件夹不存在
if (!save_ists()) {
save_file_url.mkdirs();
}
int w = Width(null);
int h = Height(null);
int new_w = 0;
int new_h = 0;
if (w <= Const.thumb_widht) {
new_w = w;
new_h = h;
} else {
if (w >= h) {
if (w > Const.thumb_widht) {
new_w = Const.thumb_widht;
new_h = h * Const.thumb_widht / w;
}
if (new_h > Const.thumb_height) {
new_w = new_w * Const.thumb_height / new_h;
new_h = Const.thumb_height;
}
} else {
new_h = Const.thumb_height;
new_w = w * Const.thumb_height / h;
}
}
BufferedImage image = new BufferedImage(new_w, new_h, BufferedImage.TYPE_3BYTE_BGR);
OutputStream os = new FileOutputStream(save_path + "\\" + originName);
JPEGImageEncoder encoder = ateJPEGEncoder(os);
String thumbURL = storePath + "/" + Const.folder_thumb + "/" + myFolderName + "/" + originName;
os.flush();
os.close();
return thumbURL;
}
/**
* 保存图⽚为浏览图
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @param fileSuffix ⽂件后缀根据⽂件后缀,判断不同的⽂件处理 tif 和 tiff 为tif处理⽅式(tif会压缩为 jpg 的图⽚)或其他的图⽚格式。
* @return ⽣成浏览图的存储路径
*/
public String saveImageBrowse(String originalURL, String storePath, String myFolderName, String originName, String fileSuffix) {
fileSuffix = LowerCase();
if ("tif".equals(fileSuffix) || "tiff".equals(fileSuffix)) {
return this.saveTifToJpgBrowse(originalURL, storePath, myFolderName, originName);
} else {
return this.saveImageBrowse(originalURL, storePath, myFolderName, originName);
return this.saveImageBrowse(originalURL, storePath, myFolderName, originName);
}
}
/**
* 普通图⽚处理为浏览图
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @return ⽣成浏览图的存储路径
*/
public String saveImageBrowse(String originalURL, String storePath, String myFolderName, String originName) {
String browseURL = "default_image.png";
try {
BufferedImage originalImage = ad(new File(Const.uploadFilePath + "/" + originalURL));
browseURL = ateImageBrowse(storePath, myFolderName, originName, originalImage);
} catch (IOException e) {
e.printStackTrace();
}
return browseURL;
}
/**
* tif 图⽚处理为浏览图 jpg 格式。
*
* @param originalURL 源⽂件路径
* @param storePath ⽂件存储⽂件夹
* @param myFolderName ⽂件存储⼦⽂件夹
* @param originName ⽂件名字
* @return ⽣成浏览图的存储路径
*/
public String saveTifToJpgBrowse(String originalURL, String storePath, String myFolderName, String originName) {
originName = originName.substring(0, originName.lastIndexOf(".")) + ".jpg";
String browseURL = "default_image.png";
ImageReader reader = null;
FileImageInputStream inputStream = null;
try {
reader = ImageReadersByFormatName("tiff").next();
inputStream = new FileImageInputStream(new File(Const.uploadFilePath + "/" + originalURL));
reader.setInput(inputStream);
BufferedImage originalImage = ad(0);
browseURL = ateImageBrowse(storePath, myFolderName, originName, originalImage);
} catch (IOException e) {
e.printStackTrace();
}
return browseURL;
}
private String createImageBrowse(String storePath, String myFolderName, String originName, BufferedImage originalImage) throws IOException { String save_path = Const.uploadFilePath + "\\" + storePath + "\\" + Const.folder_browse + "\\" + myFolderName;
File save_file_url = new File(save_path);
//⽂件夹不存在
if (!save_ists()) {
save_file_url.mkdirs();
}
int w = Width(null);
int h = Height(null);
int new_w = 0;
int new_h = 0;
if (w <= Const.browse_widht) {
new_w = w;
new_h = h;
} else {
} else {
if (w >= h) {
if (w > Const.browse_widht) {
new_w = Const.browse_widht;
new_h = h * Const.browse_widht / w;
}
if (new_h > Const.browse_height) {
new_w = new_w * Const.browse_height / new_h;
new_h = Const.browse_height;
}
} else {
new_h = Const.browse_height;
new_w = w * Const.browse_height / h;
}
}
BufferedImage image = new BufferedImage(new_w, new_h, BufferedImage.TYPE_3BYTE_BGR);
JPEGImageEncoder encoder = ateJPEGEncoder(os);
String browseURL = storePath + "/" + Const.folder_browse + "/" + myFolderName + "/" + originName;
os.flush();
os.close();
return browseURL;
}
/**
* 保存原图到服务器上。
*
* @param storePath
* @param myFolderName
* @return
* @throws IOException
*/
public UploaderFileBean saveImageOriginal(String storePath, String myFolderName) throws IOException {
String save_path = Const.uploadFilePath + "\\" + storePath + "\\" + Const.folder_original + "\\" + myFolderName;
UploaderFileBean uploadBean = new UploaderFileBean();
File save_file_url = new File(save_path);
//⽂件夹不存在
if (!save_ists()) {
save_file_url.mkdirs();
}
String originName = OriginalFilename();
String titleName = originName.substring(0, originName.lastIndexOf("."));
long fileSize = Size();
originName = System.currentTimeMillis() + "_" + originName;
String saveURL = storePath + "/" + Const.folder_original + "/" + myFolderName + "/" + originName;
File file = new File(save_path, originName);
uploadBean.setOriginalURL(saveURL);
uploadBean.setTitleName(titleName);
uploadBean.setFileName(originName);
uploadBean.setFileSize(fileSize);
ImageFileBean imageInfo = ImageOriginalInfo(file);
uploadBean.setImageInfo(imageInfo);
return uploadBean;
}
public ImageFileBean getImageOriginalInfo(File file) {
ImageFileBean imageBean = new ImageFileBean();
Metadata metadata = null;
Directory exif = null;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论