java上传图片功能,改变图片大小。

qq_20101283 2014-10-14 09:28:08
下面一段是我上传图片的功能,求大神帮我加一段,就是比如我上传一个10M的图片,改变它的大小,改小点。
/**
* @功能 保存产品图片
*/
public String saveProductImg(File file,ProductImg img,String fileFileName){

String ext = fileFileName.substring(fileFileName.lastIndexOf('.')+1).toLowerCase(); //图片后缀名

String fileName = img.getId()+"."+ext; //新文件名
String pathdir = "/upload/product/"+ img.getProduct().getId(); //保存文件夹

//保存
String realpathdir = ServletActionContext.getServletContext().getRealPath(pathdir); //绝对路径
File savedir = new File(realpathdir);
if(!savedir.exists()) savedir.mkdirs(); //如果目录不存在就新建
File filePro=new File(realpathdir, fileName);
try {
FileUtils.copyFile(file,filePro);
} catch (IOException e) {
e.printStackTrace();
}

return pathdir+"/"+fileName;
}
...全文
953 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy_think123 2014-10-15
  • 打赏
  • 举报
回复
图片压缩,如果采用zip类型的压缩的话,那么其效率确实不高,你可以采用其他的先压缩在上传,或者是切割上传,然后在后台进行组装
Magical茏 2014-10-14
  • 打赏
  • 举报
回复
1楼不是给了 方法的链接么?
qq_20101283 2014-10-14
  • 打赏
  • 举报
回复
不要沉啊~求大神现身
qq_20101283 2014-10-14
  • 打赏
  • 举报
回复
新手,在线等高手修改代码。
tony4geek 2014-10-14
  • 打赏
  • 举报
回复
原理应该是压缩
shixitong 2014-10-14
  • 打赏
  • 举报
回复
海逸_2018 2014-10-14
  • 打赏
  • 举报
回复
图片压缩工具类,点一楼给你的链接进去,有你想要的
  • 打赏
  • 举报
回复

@SuppressWarnings("finally")
    public static byte[] scale(byte[] bytes , double width, double height) {
        BufferedImage bufferedImage = null;
        BufferedImage bufTarget = null;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            bufferedImage = ImageIO.read(bais);
            double sx =  width / bufferedImage.getWidth();
            double sy =  height / bufferedImage.getHeight();
            int type = bufferedImage.getType();
            if (type == BufferedImage.TYPE_CUSTOM) {
                ColorModel cm = bufferedImage.getColorModel();
                WritableRaster raster = cm.createCompatibleWritableRaster((int)width, (int)height);
                boolean alphaPremultiplied = cm.isAlphaPremultiplied();
                bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
            } else {
                bufTarget = new BufferedImage((int)width, (int)height, type);
            }
            
            Graphics2D g = bufTarget.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g.drawRenderedImage(bufferedImage, AffineTransform.getScaleInstance(sx, sy));
            g.dispose();
            
            if(bufTarget != null){
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(bufTarget, "jpeg", baos);
                byte[] result = baos.toByteArray();
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
图片压缩,把图片的byte字节和需要重新生成的大小传递进来即可.

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧