@ResponseBody
@PostMapping("/fastuploads")
String fastuploads(@RequestParam("file") MultipartFile file) {
String url = "";
try {
String pictureByName = PfDemo.getPictureByName(file);
url = pictureByName;
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import org.springframework.web.multipart.MultipartFile;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectoryBase;
import com.project.server.clazz.FtpSeting;
import com.project.util.FtpUtil3;
import com.project.util.IDUtils;
public class PDemo {
private static String path = "服务器临时路径";
// 处理ios图片旋转的问题
public static String getPictureByName(MultipartFile file){
try {
//通过输入流获取图片数据
InputStream inStream = file.getInputStream();
BufferedImage src = ImageIO.read(inStream);
String fileName = "1234.jpg";
BufferedImage bi = null;
//图片存在
if(src != null){
//获取图片旋转角度
int angel = getRotateAngleForPhoto(file);
bi = rotateImage(src, angel);
}else{
//图片不存在
System.out.println("图片不存在");
//return "";
}
ImageIO.write(bi, "JPG", new FileOutputStream(path+fileName));
//先生成个临时文件 上传到服务器后删除
File file1 = new File(path+fileName);
//自己封装的上次服务器方法
skinImg(ftpSeting, file1, fileName);
file1.delete();
return 图片路径;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 图片翻转时,计算图片翻转到正常显示需旋转角度
*/
public static int getRotateAngleForPhoto(MultipartFile files){
// File file = new File(fileName);
int angel = 0;
Metadata metadata;
File file = null ;
try{
file = multipartFileToFile(files);
metadata = JpegMetadataReader.readMetadata(file);
Directory directory = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);
if(directory != null && directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)){
// Exif信息中方向
int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
// 原图片的方向信息
if(6 == orientation ){
//6旋转90
angel = 90;
}else if( 3 == orientation){
//3旋转180
angel = 180;
}else if( 8 == orientation){
//8旋转90
angel = 270;
}
}
file.delete();
} catch(JpegProcessingException e){
e.printStackTrace();
} catch(MetadataException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
System.out.println("图片旋转角度:" + angel);
return angel;
}
/**
* 旋转图片
*/
public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {
if (bufferedImage == null) {
return null;
}
if (angel < 0) {
// 将负数角度,纠正为正数角度
angel = angel + 360;
}
int imageWidth = bufferedImage.getWidth(null);
int imageHeight = bufferedImage.getHeight(null);
// 计算重新绘制图片的尺寸
Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);
// 获取原始图片的透明度
int type = bufferedImage.getColorModel().getTransparency();
BufferedImage newImage = null;
newImage = new BufferedImage(rectangle.width, rectangle.height, type);
Graphics2D graphics = newImage.createGraphics();
// 平移位置
graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
// 旋转角度
graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
// 绘图
graphics.drawImage(bufferedImage, null, null);
return newImage;
}
/**
* 计算旋转后的尺寸
*
* @param src
* @param angel
* @return
*/
private static Rectangle calculatorRotatedSize(Rectangle src, int angel) {
if (angel >= 90) {
if (angel / 90 % 2 == 1) {
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}