smartUpload上传问题(图片质量变差)

LittleJ 发布于 2013/02/18 10:46
阅读 1K+
收藏 0

选择上传后在tomcat里分辨率就变低了……


String userWebAppPath = getWebAppPath();
/** 检查是否有图片上传文件夹 */
checkImageDir(userWebAppPath);


/** 图片上传的相对路径 */
String imgUploadPath = null;
String imgWebAppPath = null;
/** 图片后缀 */
String imgFileExt = null;


/** 图片名称:以当前日期 */
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
String imgFileId = formatter.format(new Date());


// 图片初始化高度与宽度
String width = null;
String height = null;


int imgWidth = 0;
int imgHeight = 0;


try {


com.jspsmart.upload.SmartUpload smartUpload = new com.jspsmart.upload.SmartUpload();
smartUpload.initialize(getServletConfig(), request, response);
smartUpload.upload();
com.jspsmart.upload.Request rqest = smartUpload.getRequest();


// 指定图片宽度和高度
width = rqest.getParameter("width");
if (null == width) {
width = "700";
}
height = rqest.getParameter("height");
if (null == height) {
height = "600";
}


imgWidth = Integer.parseInt(width);
imgHeight = Integer.parseInt(height);


// 文件个数
int fileCounts = smartUpload.getFiles().getCount();


for (int i = 0; i < fileCounts; i++) {
com.jspsmart.upload.File myFile = smartUpload.getFiles()
.getFile(i);


if (!myFile.isMissing()) {


imgFileExt = myFile.getFileExt();
// 组装图片真实名称
imgFileId += i + System.currentTimeMillis() + "."
+ imgFileExt;


// 图片生成路径
imgWebAppPath = userWebAppPath + imgFileId;


// 生成图片文件
myFile.saveAs(imgWebAppPath);
// 图片的相对路径
imgUploadPath = IMGROOT + imgFileId;


// 检查图片大小
BufferedImage src = ImageIO.read(new File(imgWebAppPath)); // 读入文件


int imgSrcWidth = src.getWidth(); // 得到源图宽
int imgSrcHeight = src.getHeight(); // 得到源图长


// 重新指定大小
if (imgSrcWidth <= 600 && imgSrcHeight <= 400) {
imgWidth = imgSrcWidth;
imgHeight = imgSrcHeight;
break;
} else {
if (imgWidth > 600) {
imgWidth = (int) Math
.round(imgSrcWidth
* (1.0 - ((double) (imgSrcWidth - 600) / (double) imgSrcWidth)));
imgHeight = (int) Math
.round(imgSrcHeight
* (1.0 - ((double) (imgSrcWidth - 600) / (double) imgSrcWidth)));

if (imgHeight > 400) {
imgWidth = (int) Math
.round(imgSrcWidth
* (1.0 - ((double) (imgSrcHeight - 400) / (double) imgSrcHeight)));
imgHeight = (int) Math
.round(imgSrcHeight
* (1.0 - ((double) (imgSrcHeight - 400) / (double) imgSrcHeight)));
}
}


// 按照图片的设置大小生成
ImageCut.scale(imgWebAppPath, imgWebAppPath, imgWidth,
imgHeight);
File f = new File(imgWebAppPath);
if (f.exists()) {
System.out.println("创建" + imgWidth + "*" + imgHeight
+ "图片成功");
}
}


}


} catch (Exception ex) {
ex.printStackTrace();
}


String path = "/imgcrop.jsp?tag=0&oldImgPath=" + imgUploadPath
+ "&imgFileExt=" + imgFileExt + "&imgRoot=" + IMGROOT
+ "&width=" + imgWidth + "&height=" + imgHeight;
System.out.println("path: " + path);
request.getRequestDispatcher(path).forward(request, response);


这是网上的那个例子的上传方法(后续还有图片剪切的功能所以压小了)


怎么样才能让上传后的图片保持300dpi??

加载中
0
狂飙的小蜗牛
狂飙的小蜗牛
把分辨率调大就不会缩放压缩了
LittleJ
LittleJ
不行啊!就算不压缩大小 但是存到tomcat里的也只有96了
0
狂飙的小蜗牛
狂飙的小蜗牛
你是后台压缩的,还是在前台swf压缩的??
狂飙的小蜗牛
狂飙的小蜗牛
那你找到后台压缩的代码看看,实在不行注释掉就行啊
LittleJ
LittleJ
后台
0
LittleJ
LittleJ
这是注释后的效果
返回顶部
顶部