java获取上传图片的大小_java 图片上传的处理,获取图片大小,宽高。

jsp代码

名称

*

图片

内容

js 方法

$(form).ajaxSubmit({

type: "POST",

url:"photoes/addup",

dataType: "json",

success: function(data){

if(data.status === "ok") {

alert("保存成功");

} else {

alert(data.data);

}

}

});

js 插件地址 http://malsup.github.io/jquery.form.js

java 代码

@RequestMapping( value = "/addup", method = RequestMethod.POST, produces = "text/plain; charset=UTF-8" )

@ResponseBody

public String do_addup( @RequestParam( value = "file", required = false ) MultipartFile file, HttpServletRequest request, Photo photo )

{

try

{

String imageSavePath = "D:/Program Files"

Date now = new Date();

/**

* 设置默认图片存放的路径

*/

String fileSavePath = imageSavePath;

if( !fileSavePath.endsWith( "/" ) )

{

fileSavePath += "/";

}

SimpleDateFormat dataFormat = new SimpleDateFormat( "yyyy'/'MMdd" );

String newSaveFilePath = dataFormat.format( new Date() ) + "/";

fileSavePath += newSaveFilePath;

/**

* 目录不存在创建新目录

*/

File dir = new File( fileSavePath );

if( !dir.exists() )

{

dir.mkdirs();

}

if( file.isEmpty() )

{

throw new WrongArgumentException( "请上传图片" );

}

String[] nameArray = file.getOriginalFilename().split( "\\." );

String suffix = nameArray[nameArray.length - 1];

/**

* 生成uuid作为文件名称

*/

String fileName = UUID.randomUUID().toString().replaceAll( "-", "" ) + "." + suffix;

newSaveFilePath += fileName;

fileSavePath += fileName;

/**

* 获得文件类型(可以判断如果不是图片,禁止上传)

*/

File newFile = new File( fileSavePath );

file.transferTo( newFile );

/**

* 获取图片的宽高

*/

BufferedImage sourceImg = ImageIO.read( new FileInputStream( newFile ) );

double width = sourceImg.getWidth();

double height = sourceImg.getHeight();

double size = file.getSize();

}

catch( Exception e )

{

e.printStackTrace();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值