web之上传图片

今天写上传功能时使用了一种之前没用过的方法(该方法是从一个学习网站上面学习到的),为了防止以后遗忘,写下以记之。
模式:ssm。

1,工具类:
1)UploadImageFile.java:
public class UploadImageFile {

// 这里的属性名称image必须和页面中的增加分类部分中的type="file"的name值保持一致,如下图:
在这里插入图片描述

MultipartFile image;

public MultipartFile getImage() {
    return image;
}

public void setImage(MultipartFile image) {
    this.image = image;
}

}
2)ImageUtil.java:

public class ImageUtil {

//将图片真正的改为 jpg格式
public static BufferedImage change2jpg(File f) {
    try {
        Image i = Toolkit.getDefaultToolkit().createImage(f.getAbsolutePath());
        PixelGrabber pg = new PixelGrabber(i, 0, 0, -1, -1, true);
        pg.grabPixels();
        int width = pg.getWidth(), height = pg.getHeight();
        final int[] RGB_MASKS = { 0xFF0000, 0xFF00, 0xFF };
        final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
        DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
        WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
        BufferedImage img = new BufferedImage(RGB_OPAQUE, raster, false, null);
        return img;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

//resizeimage 改变大小
public static void resizeImage(File srcFile, int width,int height, File destFile) {
    try {
        if(!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();
        Image i = ImageIO.read(srcFile);
        i = resizeImage(i, width, height);
        ImageIO.write((RenderedImage) i, "jpg", destFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static Image resizeImage(Image srcImage, int width, int height) {
    try {

        BufferedImage buffImg = null;
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        buffImg.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        return buffImg;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

}

2)控制台:
@RequestMapping(“admin_category_add”)

public String add(Category category, HttpSession session, UploadImageFile uploadImageFile) throws IOException {

    //插入新的分类
       categoryService.add(category);

    //创建文件夹和文件
    File fileFolder = new File(session.getServletContext().getRealPath("img/category"));
    
    //这里的category.getId()为什么可以自动获取新建id?。
    // 因为:因为在Mapper.xml中的<insert id="add" keyProperty="id" useGeneratedKeys="true"></insert>。
    //使用id 作为图片名称。
    File file = new File(fileFolder, category.getId() + ".jpg");

    //判断当前文件是否存在
    if (!file.getParentFile().exists()){
        file.getParentFile().mkdirs();
    }
    //将图片放进file文件中
    uploadImageFile.getImage().transferTo(file);
    //将图片转化为jpg格式。
    BufferedImage bufferedImage = ImageUtil.change2jpg(file);

    //将图片bufferImage 以 jpg格式写进 文件file中。
    ImageIO.write(bufferedImage,"jpg",file);
    
    return "redirect:/admin_category_list";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值