[前后端分离]java:图片与缩略图上传

一、图片由一台服务器传至另一台服务器(FTP):

1.图片上传

Image img = ImageIO.read(multipartFile.getInputStream()); //multipartFile为上传的多文件中的其中一个

if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) { return new String[]{};}  //判断文件是否为图片

if (!FtpUtils.uploadFile(filePath,multipartFile.getInputStream())){ return new String[]{};}    //使用ftp上传文件,filePath 为上传路径,FtpUtils.uploadFile 为FTP上传文件方法

double size = multipartFile.getInputStream().available();//获取图片大小

size = (size/(double)1024)/(double)1024;if (size > 10){ return new String[]{};}  //限制文件大小size为MB单位

2.缩略图上传

Image image = ImageIO.read(multipartFile.getInputStream());

int width=image.getWidth(null); //获取原图宽度

int height=image.getHeight(null);//获取原图高度

int wrate=width/200; //宽度缩略图

int hrate=height/200;//高度缩略图

int rate=0;

if (wrate>hrate) { rate=wrate;}//宽度缩略图比例大于高度缩略图,使用宽度缩略图

else { rate=hrate;}

if (rate == 0){ rate = 1;}  //若原图过小,则按照原图大小存储

//计算缩略图最终的宽度和高度

int newWidth=width/rate; 

int newHeight=height/rate;

BufferedImage bufferedImage=new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

//图片缩略图实现

bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH), 0, 0, null);//*image/jpeg 

String imageType=multipartFile.getContentType().substring(multipartFile.getContentType().indexOf("/")+1);  //获取图片类型

ByteArrayOutputStream os = new ByteArrayOutputStream();     //字节输出流

ImageIO.write(bufferedImage, imageType,os);             //将缩略图字节写入到os  中

InputStream thuis = new ByteArrayInputStream(os.toByteArray());   //转换为输入流

if (!FtpUtils.uploadFile(thumbnailPath,thuis)){ return new String[]{};}  //上传ftp



3.FtpUtils工具类

public static boolean uploadFile(String fileName,InputStream in){

boolean success =false;

try{

ftp.connect(URL,PORT);    //;连接

ftp.login(USERNAME,PASSWORD);        //登录

int reply =ftp.getReplyCode();        //返回码

if (!FTPReply.isPositiveCompletion(reply))    //连接失败

{

ftp.disconnect();

return false;

}

ftp.setControlEncoding("UTF-8");

ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

ftp.enterLocalPassiveMode();

ftp.storeFile(fileName,in);

ftp.changeWorkingDirectory("/image/");//修改保存图片的文件夹,为绝对路径,'/'为FTP根目录,image为根目录下文件夹

in.close();

ftp.logout();

success =true;

}catch (Exception e){

e.printStackTrace();

}finally {

disConnection();

}

return success;

}

4.代码

protected String[] upload(HttpServletRequest request)throws IOException {

String filePaths="";

String thumbnailPaths="";

//创建一个通用的多部分解析器

 CommonsMultipartResolver multipartResolver =new CommonsMultipartResolver(request.getSession().getServletContext());

//判断 request 是否有文件上传,即多部分请求

if(multipartResolver.isMultipart(request)){

//转换成多部分request

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

//取得request中的所有文件名

Iterator iter = multiRequest.getFileNames();

while(iter.hasNext()){

List multipartFiles = multiRequest.getFiles(iter.next());

String basePath ="保存图片的绝对路径";

for (MultipartFile multipartFile : multipartFiles){

if(ArrayUtils.contains(contentTypes,multipartFile.getContentType())){

String fileName = multipartFile.getOriginalFilename();

if (StringUtils.isNotEmpty(fileName)){

fileName=fileName.substring(fileName.lastIndexOf("."));

Image img = ImageIO.read(multipartFile.getInputStream());

if (img ==null || img.getWidth(null) <=0 || img.getHeight(null) <=0) {return new String[]{};}

double size =  multipartFile.getInputStream().available();

size = (size/(double)1024)/(double)1024;

if (size >10){return new String[]{};}

String filePath= '自命名';

filePaths += basePath + filePath +"|";

String thumbnailPath= '自命名';

InputStream is=multipartFile.getInputStream();

if (!FtpUtils.getInstance(FtpUtils.TYPE_ACTIVE).uploadFile(filePath,multipartFile.getInputStream())){

return new String[]{};}

//缩略图

  Image image = ImageIO.read(multipartFile.getInputStream());

int width=image.getWidth(null);//获取原图宽度

 int height=image.getHeight(null);//获取原图高度

 int wrate=width/200;//宽度缩略图

  int hrate=height/200;//高度缩略图

 int rate=0;

if (wrate>hrate) {//宽度缩略图比例大于高度缩略图,使用宽度缩略图

rate=wrate;}else {rate=hrate;}

if (rate ==0){rate =1;}

//计算缩略图最终的宽度和高度

 int newWidth=width/rate;

int newHeight=height/rate;

BufferedImage bufferedImage=new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

//图片缩略图实现

bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH),0,0,null);

//*image/jpeg

String imageType=multipartFile.getContentType().substring(multipartFile.getContentType().indexOf("/")+1);

ByteArrayOutputStream os =new ByteArrayOutputStream();

ImageIO.write(bufferedImage, imageType,os);

InputStream thuis =new ByteArrayInputStream(os.toByteArray());

if (!FtpUtils.getInstance(FtpUtils.TYPE_ACTIVE).uploadFile(thumbnailPath,thuis)){

return new String[]{};}

thumbnailPaths += basePath + thumbnailPath +"|";

}}}}}

filePaths=filePaths.length()>1?filePaths.substring(0,filePaths.length()-1):filePaths;

thumbnailPaths=thumbnailPaths.length()>1?thumbnailPaths.substring(0,thumbnailPaths.length()-1):thumbnailPaths;

return new String[]{filePaths,thumbnailPaths};}

你可能感兴趣的:([前后端分离]java:图片与缩略图上传)