开发者社区> 问答> 正文

smartUpload上传问题(图片质量变差) 400 请求出错


选择上传后在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??

展开
收起
kun坤 2020-05-28 14:54:20 438 0
1 条回答
写回答
取消 提交回答
  • http://pan.baidu.com/share/link?shareid=266813&uk=3240181836######把分辨率调大就不会缩放压缩了######不行啊!就算不压缩大小 但是存到tomcat里的也只有96了######你是后台压缩的,还是在前台swf压缩的??######那你找到后台压缩的代码看看,实在不行注释掉就行啊######后台######这是注释后的效果

    2020-05-29 13:10:07
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
面向失败设计 立即下载
阿里巴巴代码缺陷检测探索与实践 立即下载
低代码开发师(初级)实战教程 立即下载