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();
}
}