这是我的代码
InputStream   in=null; 
ResultSet rsimg=workM.executeQuery(sql2);
if(rsimg.next()){
in=rsimg.getBinaryStream("CONTENT"); 
}
if(in!=null){          
response.reset();           
response.setContentType("image/jpeg");     
OutputStream   os=response.getOutputStream();     
byte[]  b=new byte[1024];           
int len;   
while((len=in.read(b,0,b.length))>0){
os.write(b,0,len);
}    
os.flush();       
in.close();
rsimg.close();
workM.close();
显示出来的图片是原始的大小 我想对图片进行控制 大小什么的 请问有什么办法吗?谢谢了!

解决方案 »

  1.   


    /**
        * 压缩图片方法
        * 
        * @param oldFile
        *            将要压缩的图片
        * @param width
        *            压缩宽
        * @param height
        *            压缩长
        * @param quality
        *            压缩清晰度 <b>建议为1.0</b>
        * @param smallIcon
        *            压缩图片后,添加的扩展名
        * @return
        */
        public String proce1(String oldFile, int width, int height, float quality,
                String smallIcon) {
            if (oldFile == null) {
                return null;
            }
            String newImage = null;
            try {
                File file = new File(oldFile);
                if(!file.exists()) //文件不存在时
                    return null;
                /** 对服务器上的临时文件进行处理 */
                Image srcFile = ImageIO.read(file);
                // 为等比缩放计算输出的图片宽度及高度
                double rate1 = ((double) srcFile.getWidth(null)) / (double) width
                        + 0.1;
                double rate2 = ((double) srcFile.getHeight(null)) / (double) height
                        + 0.1;
                double rate = rate1 > rate2 ? rate1 : rate2;
                int new_w = (int) (((double) srcFile.getWidth(null)) / rate);
                int new_h = (int) (((double) srcFile.getHeight(null)) / rate);
                /** 宽,高设定 */
                BufferedImage tag = new BufferedImage(new_w, new_h,
                        BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);
                String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
                /** 压缩后的文件名 */
                // newImage =smallIcon + filePrex
                // +oldFile.substring(filePrex.length());
                newImage = filePrex + smallIcon
                        + oldFile.substring(filePrex.length());
                // newImage = smallIcon;
                /** 压缩之后临时存放位置 */
                FileOutputStream out = new FileOutputStream(newImage);            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
                /** 压缩质量 */
                jep.setQuality(quality, true);
                encoder.encode(tag, jep);            out.close();
                srcFile.flush();        } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return newImage;
        }
      

  2.   

    把图片放进一个Div里面
    然后设置Div的大小。
    试试看。
      

  3.   

    谢谢啊,不过css 对div的控制根本不管用啊
      

  4.   

    File file = new File(oldFile);
    不是只能操作 硬盘上的文件吗?
    真的可以操作从数据库中输出的文件流吗?