教你在struts2上传图片生成高清缩略图的功能 运用在spring MVC上,各们亲!教你举一反三:
近段时间在运用了spring mvc+spring3.x+mybatis3.x框架开发项目,碰到一个问题,上传图片并生成缩略图。在网上找了很多资料都没有看到我想要的功能,要不就单纯上传图片,而且代码简单没什么实用处。要不就是上传文件,不是我想找的。记得之前struts2也有上传图片生成高清缩略图的功能,于是看了看,有两种方法,不过大同小异。代码如下
以下是struts2也有上传图片哦。 如果要看 spring mvc上传图片 请直接拉下下
第一种:清晰度不高;
UploadAction
package com.lanyuan.upload;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = -8204063374280945416L;
private File upload;// 路径
private String uploadFileName;// 原文件名
private String uploadContentType;// 文件类型
public String upload() throws Exception {
UploadUtil uploadutil = new UploadUtil();
uploadutil.uploadImage1(getUpload(), getUploadContentType(), getUploadFileName());
return "success";
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
/**
*
* 上传图片 工具类
* 大图片路径,生成小图片路径,
* 大图片文件名,生成小图片文名,
* 生成小图片宽度,生成小图片高度,
* 是否等比缩放(默认为true))
*
* @author Administrator
*
*/
UploadUtil
package com.lanyuan.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
/**
*
* 上传图片 工具类
* 大图片路径,生成小图片路径,
* 大图片文件名,生成小图片文名,
* 生成小图片宽度,生成小图片高度,
* 是否等比缩放(默认为true))
*
* @author Administrator
*
*/
public class UploadUtil {
private String imagePath = "/image/"+new SimpleDateFormat("yyyyMMddHH").format(new Date())+"";// 配置图片路径
private String image_smallPath = "/image_small/"+new SimpleDateFormat("yyyyMMddHH").format(new Date())+"";// 配置小图片路径
/**
*
* @param request /
* @param getUpload 路径
* @param getUploadContentType 文件类型
* @param getUploadFileName 原文件名
* @return
* @throws IOException
*/
public void uploadImage1(File getUpload, String getUploadContentType, String getUploadFileName) throws IOException {
String getImagePath = ServletActionContext.getServletContext().getRealPath(imagePath);
String getImage_smallPath = ServletActionContext.getServletContext().getRealPath(image_smallPath);
File image = new File(getImagePath);
if (!image.exists()) {
image.mkdir();
}
File image_small = new File(getImage_smallPath);
if (!image_small.exists()) {
image_small.mkdir();
}
// 得到上传文件的后缀名
String uploadName = getUploadContentType;
System.out.println("图片类型 ------------"+uploadName);
String lastuploadName = uploadName.substring(
uploadName.indexOf("/") + 1, uploadName.length());
System.out.println("得到上传文件的后缀名 ------------"+lastuploadName);
// 得到文件的新名字
String fileNewName = generateFileName(getUploadFileName);
System.out.println("// 得到文件的新名字 ------------"+fileNewName);
FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);
FileInputStream fis = new FileInputStream(getUpload);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
DwindlePic mypic = new DwindlePic();
mypic.s_pic(getImagePath + "/", getImage_smallPath + "/", fileNewName, fileNewName, 110, 110, false);
//最后返回图片路径
imagePath = imagePath+"/"+fileNewName;
image_smallPath = image_smallPath+"/"+fileNewName;
}
/**
* 传入原图名称,,获得一个以时间格式的新名称
* @param fileName 原图名称
* @return
*/
private String generateFileName(String fileName) {
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
public String getImagepath() {
return imagePath;
}
public String getImageSmallpath() {
return image_smallPath;
}
}
大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
DwindlePic
package com.lanyuan.upload;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.applet.*;
/**
* s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
*
* @author Administrator
*
*/
public class DwindlePic {
String InputDir; // 输入图路径
String OutputDir; // 输出图路径
String InputFileName; // 输入图文件名
String OutputFileName; // 输出图文件名
int OutputWidth = 110; // 默认输出图片宽
int OutputHeight = 110; // 默认输出图片高
int rate = 0;
boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
public DwindlePic() {
// 初始化变量
InputDir = "";
OutputDir = "";
InputFileName = "";
OutputFileName = "";
OutputWidth = 110;
OutputHeight = 110;
rate = 0;
}
public boolean s_pic() {
// BufferedImage image;
// String NewFileName;
// 建立输出文件对象
File file = new File(OutputDir + OutputFileName);
FileOutputStream tempout = null;
try {
tempout = new FileOutputStream(file);
} catch (Exception ex) {
System.out.println(ex.toString());
}
Image img = null;
Toolkit tk = Toolkit.getDefaultToolkit();
Applet app = new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img = tk.getImage(InputDir + InputFileName);
mt.addImage(img, 0);
mt.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return false;
} else {
int new_w;
int new_h;
if (this.proportion == true) { // 判断是否是等比缩放.
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) OutputWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) OutputHeight + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) (((double) img.getWidth(null)) / rate);
new_h = (int) (((double) img.getHeight(null)) / rate);
} else {
new_w = OutputWidth; // 输出的图片宽度
new_h = OutputHeight; // 输出的图片高度
}
BufferedImage buffImg = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, new_w, new_h);
g.drawImage(img, 0, 0, new_w, new_h, null);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try {
encoder.encode(buffImg);
tempout.close();
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
return true;
}
public boolean s_pic(String InputDir, String OutputDir,
String InputFileName, String OutputFileName) {
// 输入图路径
this.InputDir = InputDir;
// 输出图路径
this.OutputDir = OutputDir;
// 输入图文件名
this.InputFileName = InputFileName;
// 输出图文件名
this.OutputFileName = OutputFileName;
return s_pic();
}
/**
* s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
* @param InputDir
* @param OutputDir
* @param InputFileName
* @param OutputFileName
* @param width
* @param height
* @param gp
* @return
*/
public boolean s_pic(String InputDir, String OutputDir,
String InputFileName, String OutputFileName, int width, int height,
boolean gp) {
// 输入图路径
this.InputDir = InputDir;
// 输出图路径
this.OutputDir = OutputDir;
// 输入图文件名
this.InputFileName = InputFileName;
// 输出图文件名
this.OutputFileName = OutputFileName;
// 设置图片长宽
setW_H(width, height);
// 是否是等比缩放 标记
this.proportion = gp;
return s_pic();
}
public void setInputDir(String InputDir) {
this.InputDir = InputDir;
}
public void setOutputDir(String OutputDir) {
this.OutputDir = OutputDir;
}
public void setInputFileName(String InputFileName) {
this.InputFileName = InputFileName;
}
public void setOutputFileName(String OutputFileName) {
this.OutputFileName = OutputFileName;
}
public void setOutputWidth(int OutputWidth) {
this.OutputWidth = OutputWidth;
}
public void setOutputHeight(int OutputHeight) {
this.OutputHeight = OutputHeight;
}
public void setW_H(int width, int height) {
this.OutputWidth = width;
this.OutputHeight = height;
}
}
看看效果吧!
第二种,清晰度还可以接受
package com.xiyuan.upload;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.struts2.ServletActionContext;
/**
*
* 上传图片 工具类 大图片路径,生成小图片路径, 大图片文件名,生成小图片文名, 生成小图片宽度,生成小图片高度, 是否等比缩放(默认为true))
*
* @author Administrator
*
*/
public class UploadUtil
{
private String imagePath = "/imageFile/" + new SimpleDateFormat("yyyyMMddHH").format(new Date()) + "";// 配置图片路径
/**
*
* @param getUpload
* 路径
* @param getUploadContentType
* 文件类型
* @param getUploadFileName
* 原文件名
* @return
* @throws IOException
*/
public void uploadImage1(File getUpload, String getUploadContentType, String getUploadFileName) throws IOException
{
String getImagePath = ServletActionContext.getServletContext().getRealPath(imagePath);
File image = new File(getImagePath);
if (!image.exists())
{
image.mkdir();
}
// 得到上传文件的后缀名
String uploadName = getUploadContentType;
System.out.println("图片类型 ------------" + uploadName);
String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());
System.out.println("得到上传文件的后缀名 ------------" + lastuploadName);
// 得到文件的新名字
String fileNewName = generateFileName(getUploadFileName);
System.out.println("// 得到文件的新名字 ------------" + fileNewName);
// FileOutputStream fos = new FileOutputStream(getImagePath + "/" +
// fileNewName);
//
// FileInputStream fis = new FileInputStream(getUpload);
// byte[] buffer = new byte[1024];
// int len = 0;
// while ((len = fis.read(buffer)) > 0) {
// fos.write(buffer, 0, len);
// }
// 最后返回图片路径
imagePath = imagePath + "/" + fileNewName;
System.out.println(" 回图片路径 " + getUpload);
System.out.println(" //最后返回图片路径 " + imagePath);
BufferedImage srcBufferImage = ImageIO.read(getUpload);
System.out.println(" w " + srcBufferImage.getWidth() + " w " + srcBufferImage.getHeight());
BufferedImage scaledImage;
ScaleImage scaleImage = ScaleImage.getInstance();
int yw = srcBufferImage.getWidth();
int yh = srcBufferImage.getHeight();
int w = 110, h = 110;
// 如果上传图片 宽高 比 压缩的要小 则不压缩
if (w > yw && h > yh)
{
FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);
FileInputStream fis = new FileInputStream(getUpload);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
}
else
{
scaledImage = scaleImage.imageZoomOut(srcBufferImage, w, h);
FileOutputStream out = new FileOutputStream(getImagePath + "/" + fileNewName);
ImageIO.write(scaledImage, "jpeg", out);
}
}
/**
* 传入原图名称,,获得一个以时间格式的新名称
*
* @param fileName
* 原图名称
* @return
*/
private String generateFileName(String fileName)
{
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
public String getImagepath()
{
return imagePath;
}
}
ScaleImage
package com.xiyuan.upload;
import java.awt.image.BufferedImage;
public class ScaleImage {
private int width;
private int height;
private int scaleWidth;
private double support = (double) 3.0;
private double PI = (double) 3.14159265358978;
private double[] contrib;
private double[] normContrib;
private double[] tmpContrib;
private int nDots;
private int nHalfDots;
/**
* Start: Use Lanczos filter to replace the original algorithm for image
* scaling. Lanczos improves quality of the scaled image modify by :blade
*/
private static ScaleImage instance = new ScaleImage();
private ScaleImage(){};
public static ScaleImage getInstance(){
return instance;
}
public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {
width = srcBufferImage.getWidth();
height = srcBufferImage.getHeight();
scaleWidth = w;
if (DetermineResultSize(w, h) == 1) {
return srcBufferImage;
}
CalContrib();
BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
return pbFinalOut;
}
/**
* 决定图像尺寸
*/
private int DetermineResultSize(int w, int h) {
double scaleH, scaleV;
scaleH = (double) w / (double) width;
scaleV = (double) h / (double) height;
// �?��判断�?��scaleH,scaleV,不做放大操�?
if (scaleH >= 1.0 && scaleV >= 1.0) {
return 1;
}
return 0;
} // end of DetermineResultSize()
private double Lanczos(int i, int inWidth, int outWidth, double Support) {
double x;
x = (double) i * (double) outWidth / (double) inWidth;
return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)
/ (x * PI / Support);
} // end of Lanczos()
//
// Assumption: same horizontal and vertical scaling factor
//
private void CalContrib() {
nHalfDots = (int) ((double) width * support / (double) scaleWidth);
nDots = nHalfDots * 2 + 1;
try {
contrib = new double[nDots];
normContrib = new double[nDots];
tmpContrib = new double[nDots];
} catch (Exception e) {
System.out.println("init contrib,normContrib,tmpContrib" + e);
}
int center = nHalfDots;
contrib[center] = 1.0;
double weight = 0.0;
int i = 0;
for (i = 1; i <= center; i++) {
contrib[center + i] = Lanczos(i, width, scaleWidth, support);
weight += contrib[center + i];
}
for (i = center - 1; i >= 0; i--) {
contrib[i] = contrib[center * 2 - i];
}
weight = weight * 2 + 1.0;
for (i = 0; i <= center; i++) {
normContrib[i] = contrib[i] / weight;
}
for (i = center + 1; i < nDots; i++) {
normContrib[i] = normContrib[center * 2 - i];
}
} // end of CalContrib()
// 处理边缘
private void CalTempContrib(int start, int stop) {
double weight = 0;
int i = 0;
for (i = start; i <= stop; i++) {
weight += contrib[i];
}
for (i = start; i <= stop; i++) {
tmpContrib[i] = contrib[i] / weight;
}
} // end of CalTempContrib()
private int GetRedValue(int rgbValue) {
int temp = rgbValue & 0x00ff0000;
return temp >> 16;
}
private int GetGreenValue(int rgbValue) {
int temp = rgbValue & 0x0000ff00;
return temp >> 8;
}
private int GetBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}
private int ComRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
}
// 行水平滤�?
private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX,
int start, int stop, int y, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startX, j = start; i <= stopX; i++, j++) {
valueRGB = bufImg.getRGB(i, y);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
return valueRGB;
} // end of HorizontalFilter()
// 图片水平滤波
private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {
int dwInW = bufImage.getWidth();
int dwInH = bufImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iOutW, dwInH,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < iOutW; x++) {
int startX;
int start;
int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);
int y = 0;
startX = X - nHalfDots;
if (startX < 0) {
startX = 0;
start = nHalfDots - X;
} else {
start = 0;
}
int stop;
int stopX = X + nHalfDots;
if (stopX > (dwInW - 1)) {
stopX = dwInW - 1;
stop = nHalfDots + (dwInW - 1 - X);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of HorizontalFiltering()
private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY,
int start, int stop, int x, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startY, j = start; i <= stopY; i++, j++) {
valueRGB = pbInImage.getRGB(x, i);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
// System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-");
//
// System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-");
// System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->");
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
// System.out.println(valueRGB);
return valueRGB;
} // end of VerticalFilter()
private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {
int iW = pbImage.getWidth();
int iH = pbImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iW, iOutH,
BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < iOutH; y++) {
int startY;
int start;
int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);
startY = Y - nHalfDots;
if (startY < 0) {
startY = 0;
start = nHalfDots - Y;
} else {
start = 0;
}
int stop;
int stopY = Y + nHalfDots;
if (stopY > (int) (iH - 1)) {
stopY = iH - 1;
stop = nHalfDots + (iH - 1 - Y);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of VerticalFiltering()
private int Clip(int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}
/**
* End: Use Lanczos filter to replace the original algorithm for image
* scaling. Lanczos improves quality of the scaled image modify by :blade
*/
}
呵呵,,第二种不错吧! 好了,以上是struts2上传图片生成高清缩略图,接下来就是如何利用spring mvc 来完成这个功能了。。
package com.lanyuan.upload;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
/**
*
* 上传图片 工具类 大图片路径,生成小图片路径, 大图片文件名,生成小图片文名, 生成小图片宽度,生成小图片高度, 是否等比缩放(默认为true))
*
* @author Administrator
*
*/
public class UploadUtil
{
private String imagePath = "/imageFile/" + new SimpleDateFormat("yyyyMMddHH").format(new Date()) + "";// 配置图片路径
/**
*
* @param getUpload
* 路径
* @param getUploadContentType
* 文件类型
* @param getUploadFileName
* 原文件名
* @return
* @throws IOException
*/
public void uploadImage1(HttpServletRequest request,MultipartFile file, String getUploadContentType, String getUploadFileName) throws IOException
{
String getImagePath = request.getSession().getServletContext().getRealPath(imagePath);
File image = new File(getImagePath);
if (!image.exists())
{
image.mkdir();
}
// 得到上传文件的后缀名
String uploadName = getUploadContentType;
System.out.println("图片类型 ------------" + uploadName);
String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());
System.out.println("得到上传文件的后缀名 ------------" + lastuploadName);
// 得到文件的新名字
String fileNewName = generateFileName(getUploadFileName);
System.out.println("// 得到文件的新名字 ------------" + fileNewName);
// FileOutputStream fos = new FileOutputStream(getImagePath + "/" +
// fileNewName);
//
// FileInputStream fis = new FileInputStream(getUpload);
// byte[] buffer = new byte[1024];
// int len = 0;
// while ((len = fis.read(buffer)) > 0) {
// fos.write(buffer, 0, len);
// }
// 最后返回图片路径
imagePath = imagePath + "/" + fileNewName;
System.out.println(" 回图片路径 " + file.getInputStream());
System.out.println(" //最后返回图片路径 " + imagePath);
BufferedImage srcBufferImage = ImageIO.read(file.getInputStream());
System.out.println(" w " + srcBufferImage.getWidth() + " w " + srcBufferImage.getHeight());
BufferedImage scaledImage;
ScaleImage scaleImage = ScaleImage.getInstance();
int yw = srcBufferImage.getWidth();
int yh = srcBufferImage.getHeight();
int w = 400, h = 300;
// 如果上传图片 宽高 比 压缩的要小 则不压缩
if (w > yw && h > yh)
{
FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);
FileInputStream fis = (FileInputStream) file.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
}
else
{
scaledImage = scaleImage.imageZoomOut(srcBufferImage, w, h);
FileOutputStream out = new FileOutputStream(getImagePath + "/" + fileNewName);
ImageIO.write(scaledImage, "jpeg", out);
}
}
/**
* 传入原图名称,,获得一个以时间格式的新名称
*
* @param fileName
* 原图名称
* @return
*/
private String generateFileName(String fileName)
{
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
public String getImagepath()
{
return imagePath;
}
}
最后如何调用:请看:很简单
@RequestMapping ("upload")
public String upload(Model model,HttpServletRequest request)
{
//转型为MultipartHttpRequest(重点的所在)
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获得第1张图片(根据前台的name名称得到上传的文件)
MultipartFile imgFile1 = multipartRequest.getFile("imgFile");
UploadUtil uploadutil = new UploadUtil();
String fileName = imgFile1.getOriginalFilename();
try {
uploadutil.uploadImage1(request, imgFile1, imgFile1.getContentType(), fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:query";
}
亲:看到要顶哦!好辛苦研究出来的:
转载请说明出处:http://blog.csdn.net/mmm333zzz/article/details/8569637 谢谢