java 上传文件

package com.cosw.szt.manage.web.controller.ticketmanage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class SavePic {
//保存图片操作。
public static String savePic(MultipartFile multipartFile, String filePath) throws Exception {
//File file = new File(picName);
//String name = picName.substring(picName.lastIndexOf("."));

// 新文件名
int random = (int) (Math.random() * 10000);
String fileName = System.currentTimeMillis() + random + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."));

//if (name.equals(".jpg") || name.equals(".gif")) {
//InputStream input = new FileInputStream(file);
//BufferedImage bi = ImageIO.read(input);
//if (bi != null) {
if (htmlUpload(multipartFile.getInputStream(), filePath, "BIG_" + fileName)) {
System.out.println("上传成功!");
uploadJPGfile(multipartFile.getInputStream(), filePath, "SMALL_" + fileName);
} else {
System.out.println("上传失败!");
}
//} else {
//System.out.println("你选择的图片错误或者该图片已经损坏!"); // 给出提示
//}
return fileName;
}


//正常大小
private static boolean htmlUpload(InputStream inputStream,String filePath, String fileName) {
try {
InputStream input = inputStream;

byte[] buff = new byte[4096];
FileOutputStream output = new FileOutputStream(filePath + fileName);
int bytecount = 1;
while ((bytecount = input.read(buff, 0, 4096)) != -1) {
output.write(buff, 0, bytecount);
}
output.flush();
output.close();
return true;
} catch (Exception e) {
return false;
}
}
//小图片
public static boolean uploadJPGfile(InputStream inputStream,String filePath, String fileName) throws Exception {

int new_w = 150;
int new_h = 100;
BufferedImage tag = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(ImageIO.read(inputStream), 0, 0, new_w, new_h, null);
FileOutputStream out = new FileOutputStream(filePath + fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
return true;
}
}


在控制器中的处理
public String save(@RequestParam("picNameFile") MultipartFile picNameFile){
if(!picNameFile.isEmpty()){
// 上传文件存放的路径
String filePath = PropertyUtil.getProperty("upload.image.root-path")
+System.getProperty("file.separator")+"commodity"+System.getProperty("file.separator");
String fileName = SavePic.savePic(picNameFile,filePath);
//保存图片地址。暂时路径+文件名
ticket.setPicName(filePath + "BIG_" + fileName);
ticket.setImgName(filePath + "SMALL_" + fileName);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值