|
      
- 帖子
- 2379
- 主题
- 17
- 精华
- 8
- 积分
- 4933
- 疯狂金币
- 1875
- 疯狂水晶
- 11
- 在线时间
- 759 小时
  
|
《整合struts,hibernate,spring应用开发详解》中的问题
在《整合struts,hibernate,spring整合应用开发详解》的第11章中,对于
AddPhotoAction 中使用upload工具类中的问题
对于如下代码
-
- //创建一个上传相片的工具类
- Upload upload = new Upload();
- //执行上传文件
- upload.upload(file, clientPath, suffix);
- //使用工具类生成一个小图
- upload.makeImage(upload.getUrl(), 140, -20, upload.makeNewUrl(clientPath, suffix, "_small"), suffix.substring(1));
- //使用工具类生成一个大图
- upload.makeImage(upload.getUrl(), 600, -20, upload.makeNewUrl(clientPath, suffix, "_big"), suffix.substring(1)); cs.addPhoto(name, desc, 0, upload.makeNewUrl(path, suffix, ""), upload.makeNewUrl(path, suffix, "_big"),
-
- upload.makeNewUrl(path, suffix, "_small"), DateUtil.getFormalTime(), false, albumId);
复制代码
第一个问题
对于调用makeImage方法,为什么设置新的图片度度,都要设置负数-20? 不是正数? 生成大图和小图,生成的新高度都取同一个值-20呢?
第二个问题
在upload.java类中对于makeImage()方法(Upload.java代码见最后面附上)
为什么高比宽大,就非要交换?交换了为什么就会是生成的图片的长和宽都在一个范围内呢?不太明白这句话的描述意识?
- //读取图片
- BufferedImage bi = ImageIO.read(new File(url));
- //判断读入图片的宽和高
- if (bi.getHeight() > bi.getWidth())
- {
- //如果高比宽大,就交换两值,确保生成的图片的长和宽都在一个范围内
- int tmp = newWidth;
- newWidth = newHeight;
- newHeight = tmp;
- }
复制代码
第三个问题
在upload.java类中对于makeImage()方法(Upload.java代码见最后面附上)
对于如下代码
- //以新的高和宽构造一个新的缓存图片
- BufferedImage bi3 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics g = bi3.getGraphics();
-
- //在新的缓存图片中画图
- g.drawImage(image2, 0, 0, null);
复制代码
在新的缓存图片中画图 ,已经构造了一个新的缓存图片,为什么还要在新的缓存图片中画图呢?
- public class Upload
- {
- private String url;
- private String fileName;
-
- /** 上传文件方法
- * @param FormFile file, 选定文件的实体
- * @param request HttpServletRequest
- * @param userName 用户的名称,String型
- * @param webPath 服务器的绝对路径
- * @param suffix 传入文件的后缀
- */
- public void upload(FormFile file, String path, String suffix)
- throws Exception
- {
-
- //获取文件的大小
- //这里获取文件的大小,说得很不准确,应该说获取文件中的具体内容才对
- byte[] content = file.getFileData();
- //以时间格式生成文件名
- String url = makeUrl(path, suffix);
-
- //此处url是得到完整的文件路径
- this.url = url;
- //用输出流保存文件(输出流保存的文件应该是保存到硬盘上了found by heyitang 200811070923)
- FileOutputStream fos = new FileOutputStream(url);
- //写入时间
- fos.write(content);
- //关闭输出流
- fos.close();
- }
- //如下方法是得到原始的文件
- private String makeUrl(String path, String suffix)
- {
- this.fileName = DateUtil.getStringTime();
- return path + "\\" + this.fileName + suffix;
- }
- //如下方法是得到缩小和放大的图片文件
- public String makeNewUrl(String path, String suffix, String other)
- {
- return path + "\\" + this.fileName + other + suffix;
- }
- /** 返回上传后文件的绝对URL
- * @return String
- */
- public String getUrl()
- {
- return this.url;
- }
- public String getFileName()
- {
- return this.fileName;
- }
- /** 用于生成上传完后的图片的副本
- * @param url 原图的绝对URL
- * @param newWidth 生成副本的新宽度
- * @param newHeight 生成副本的新高度
- * @param newUrl 生成副本的地址
- * @param formatName 生成图片的格式
- */
- public void makeImage(String url, int newWidth, int newHeight, String newUrl, String formatName)
- throws Exception
- {
- //读取图片
- BufferedImage bi = ImageIO.read(new File(url));
- //判断读入图片的宽和高
- if (bi.getHeight() > bi.getWidth())
- {
- //如果高比宽大,就交换两值,确保生成的图片的长和宽都在一个范围内
- int tmp = newWidth;
- newWidth = newHeight;
- newHeight = tmp;
- }
- //用Image里的方法对图片进行等比压缩,只要宽和高其一值为负,则以正的那个值为最大边进行等比压缩
- Image image2 = bi.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
-
- //获取压缩后图片的高和宽
- int height = image2.getHeight(null);
- int width = image2.getWidth(null);
-
- //FileOutputStream fos = new FileOutputStream(new File(newUrl));
-
- //以新的高和宽构造一个新的缓存图片
- BufferedImage bi3 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics g = bi3.getGraphics();
-
- //在新的缓存图片中画图
- g.drawImage(image2, 0, 0, null);
-
- //构造IO流输出到文件
- FileOutputStream fos = new FileOutputStream(new File(newUrl));
- ImageIO.write(bi3, formatName, fos);
- fos.close();
- }
- /** 该方法是删除图片
- * @param url 删除图片的绝对地址
- * @param bigUrl 数据库中大图片的URL
- * @param smallUrl 数据库小图片的URL
- * @param realPath 服务器中的绝对路径
- */
- public void deleteImage(String url, String bigUrl, String smallUrl, String realPath)
- {
- //构造三个文件对象,调用方法把所有对应的图片删除
- File fUrl = new File(realPath + "/" + url);
- File fBigUrl = new File(realPath + "/" + bigUrl);
- File fSmallUrl = new File(realPath + "/" + smallUrl);
- fUrl.delete();
- fBigUrl.delete();
- fSmallUrl.delete();
- }
- }
-
复制代码 |
提问赏金:3金币
获奖名单 :
boxiong(1金币) kongyeeku(2金币)
|