图片上传的工具类:生成缩略图,文件流转换。

可与struts2集成使用

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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

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

public class FileUtil {

private static Log log = LogFactory.getLog(FileUtil.class);

public static byte[] toByteArray(File photo) throws IOException

{

FileInputStream fis = new FileInputStream(photo);

BufferedInputStream bis = new BufferedInputStream(fis);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int c = bis.read();

while (c != -1) {

baos.write(c);

c = bis.read();

}

bis.close();

byte[] rtn = baos.toByteArray();

baos.close();

return rtn;

}

public static byte[] buildThumbnail(File srcFile) {

byte[] rtn = null;

try {

Image src = ImageIO.read(srcFile); // 构造Image对象

int oldWidth = src.getWidth(null); // 得到源图宽

int oldHeight = src.getHeight(null);// 得到源图高

log.debug("old width is " + oldWidth);

log.debug("old height is " + oldHeight);

float divWidth = 200f; // 限制宽度为200

int newWidth = 200; // 缩略图宽,

int newHeight = 0; // 缩略图高

float tmp;

if (oldWidth > newWidth) {

tmp = oldWidth / divWidth;

newWidth = Math.round(oldWidth / tmp);// 计算缩略图高

newHeight = Math.round(oldHeight / tmp);// 计算缩略图高

log.debug("tmp scale is " + tmp);

} else {

newWidth = oldWidth;

newHeight = oldHeight;

}

int imageHeight = 100;

int imageWidth = 200;

log.debug("new width is " + newWidth);

log.debug("new height is " + newHeight);

BufferedImage bufferedImage = new BufferedImage(imageWidth,

imageHeight, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics2D = (Graphics2D)

bufferedImage.createGraphics();

graphics2D.setBackground(Color.WHITE);

graphics2D.clearRect(0, 0, imageWidth, imageHeight);

bufferedImage.getGraphics().drawImage(src,

//(imageWidth - oldWidth) / 2, (imageHeight - newHeight) /

2,

0,0,

newWidth, newHeight, null); // 绘制缩小后的图

ByteArrayOutputStream baos = new ByteArrayOutputStream();

BufferedOutputStream bos = new BufferedOutputStream(baos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

encoder.encode(bufferedImage); // 近JPEG编码

rtn = baos.toByteArray();

bos.close();

baos.close();

} catch (Exception e) {

log.debug(e);

} finally {

}

return rtn;

}

}

图片上传工具 java_图片上传工具类-fileUtil相关推荐

  1. 微信上传素材 java_微信上传素材php代码

    $url:微信上传媒体素材的api接口. $path_img:服务器文件的绝对路径 这里的绝对路径不是$_FILES中的临时路径,不然会报文件类型错误的异常. private function Htt ...

  2. 【VUE】Toast-UI/Editor工具实现图片自定义上传到服务器

    Toast-UI/Editor工具实现图片自定义上传到服务器 author: jwensh date: 2021.06.17 文章目录 Toast-UI/Editor工具实现图片自定义上传到服务器 所 ...

  3. 安卓okhttp上传jason和图片_微信图片总是「已过期或被清理」?简单 3 招,可摆脱烦恼...

    微信图片总是「已过期或被清理」?简单 3 招可摆脱烦恼 除了文字.表格.PPT 和 PDF,大家日常办公中也常常和「图片文件」打交道. 于是,我们总碰到这样的问题: 1)图片文件太大,在线传输耗时,甲 ...

  4. ueditor1.4.3配置过程(包含单独上传文件以及图片的使用)

    这里使用的是ueditor1.4.3的jsp版本的UTF-8版本. 首先下载相应的ueditor,将ueditor文件夹直接拷贝到项目中,文件结构如下所示: 然后将项目要用的jar包导入到lib目录下 ...

  5. mui ajax 文件上传,MUI的图片上传和压缩

    MUI的上传图片目前知道有两种方式,一种见前面文章中关于图片裁切时使用的base64作为字符串上传,另外一种则是本篇章中所使用的plus.uploader方式,它是一种真正意义上的文件上传,我们可以使 ...

  6. Spring boot上传文件(图片)到阿里云OSS(直接上传到应用服务器)

    Spring boot上传文件(图片)到阿里云OSS(直接上传到应用服务器) 主要思路 上传图片的思路有两种: 第一种是上传到应用服务器,再由应用服务器上传到OSS.这种适合上传的文件较小,较为简单, ...

  7. dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库....

    http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...

  8. 微信小程序-从相册获取图片,视频 使用相机拍照,录像上传+服务器(nodejs版)接收

    在本文 微信小程序-从相册获取图片 使用相机拍照 本地图片上传之前需要看看 微信小程序-获取用户session_key,openid,unionid - 后端为nodejs 代码封装是在上文添加的. ...

  9. OkGo上传文件、图片的用法

    在Android开发过程中,肯定会遇到与后端进行交互的地方.手机端与后端进行交互,难免会使用到OkGo.在最近的一个项目中,明明是按照官方教程写的代码,但是始终会报一个异常. 报错信息: networ ...

最新文章

  1. nGrinder性能测试框架安装步骤
  2. jq输出文本_jQuery获取文本节点之 text()/val()/html() 方法区别
  3. Java集合篇:LinkedList源码分析
  4. python秒转化为时间格式_Python耗费时间秒转 天小时分钟秒 时间格式美化
  5. php下的原生ajax请求
  6. 3.4 内置函数(1)
  7. AD19无法生成PCB_对PCB印制线的传输线效应以及封装、连接器和电缆的频率响应进行全面分析...
  8. 使用UTL_SMTP包发送邮件
  9. Pyhton学习——Day60
  10. 两个表里数组字段的映射关联(stuff ,charindex,for xml path)
  11. 拓端tecdat|R语言中Gibbs抽样的Bayesian贝叶斯简单线性回归
  12. python从入门到_python从入门到项目实践 (明日科技) 配套视频教程+源码
  13. exchange 服务器设置自动答复,Exchange自动回复设置配置
  14. rfc3315_DHCPv6-RFC3315(中文).pdf
  15. 03_STM32新建工程
  16. 微信小程序开发者工具无法登录 不显示登录二维码
  17. STM32单片机点亮流水灯
  18. 项目管理中软件项目文档的分类管理
  19. ROS分布式多机通信
  20. 渐变折射率(GRIN)透镜的建模

热门文章

  1. 【Flink】flink on yarn 并行度设置高导致任务失败
  2. 【MyBatis】MyBatis Order By 字段动态动态排序
  3. 【Logstash】logstash 报错 logstash cannot write event to DLQ reached maxQueueSize of
  4. spark学习-61-源代码:ShutdownHookManager虚拟机关闭钩子管理器
  5. 95-290-035-源码-内存管理-原理与实现:内存管理
  6. mac终端shell颜色
  7. Spark学习-SparkSQL--04-SparkSQL CLI 单节点运行与yarn集群运行
  8. 云计算学习路线图课件:云计算和虚拟机有什么区别?
  9. mysql php 时间比较大小_mysql中日期大小比较实例
  10. 分享一个入门级实操项目,思路非常棒!