if(getImagefile()!=null && getImagefile().length()>0){

String imagefilepathdir = "/images/product/"+ productinfo.getType().getTypeid()+ "/"+ productid+ "/prototype";//构建图片保存的目录
//得到图片保存的真实路径
String imagefilerealpathdir = ServletActionContext.getServletContext().getRealPath(imagefilepathdir);

File imagesavedir = new File(imagefilerealpathdir);
if(!imagesavedir.exists()) 
imagesavedir.mkdirs(); //如果文件不存在就创建

String imageext = imagefileFileName.substring(imagefileFileName.lastIndexOf('.'));

String imagename = UUID.randomUUID().toString()+imageext;

FileInputStream fileinputstream = new FileInputStream(imagefile);
BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
FileOutputStream fileoutputstream = new FileOutputStream(new File(imagefilerealpathdir,imagename));
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(fileoutputstream);

byte[] buf = new byte[1024];
int len=-1;
while((len=bufferedinputstream.read(buf)) != -1){
fileoutputstream.write(buf,0,len);
}
bufferedoutputstream.close();
fileoutputstream.close(); String path = imagefilepathdir+"/"+imagename;
String imagefilepathdir140 = "/images/product/"+ productinfo.getType().getTypeid()+ "/"+ productid+ "/140x";//构建140图片保存的目录
String imagefilerealpathdir140 = ServletActionContext.getServletContext().getRealPath(imagefilepathdir140);
File imagesavedir140 = new File(imagefilerealpathdir140);
if(!imagesavedir140.exists()) 
imagesavedir140.mkdirs(); //如果文件不存在就创建
File oldfile = new File(imagefilerealpathdir,imagename);
File newfile = new File(imagefilerealpathdir140,imagename);
ImageSizer.resize(oldfile, newfile, 140, imageext);//压缩类
可以正常的上传图片,但是压缩得到的图片是0k的。把oldfile换成“D:\\1.jpg”就可以,我感觉是Oldfile这里得不到图片,但是System.out.println(oldfile); 也可以得到oldfile的长度啊困惑中

解决方案 »

  1.   

    ImageSizer 这个工具类是没问题的,因为Junit测试是可以正常压缩图片的
      

  2.   

    jsp运行时的路径和你项目里的路径是不一样的。你用什么作容器..应该是路径问题.. 你自己仔细检查下.
      

  3.   

    我用System.out.println(oldfile.length)可以得到这个文件的大小,那就不应该是路径的问题了吧
      

  4.   


    String path1 = "e:\\";
    String name1 = "1.jpg";
    File file3 = new File(path1,name1);
    File file2 = new File("e:\\1.jpg");
    System.out.println("file3.length="+file3.length());
    System.out.println("file2.length="+file2.length());
    System.out.println(file2==file3);
    上面的打印结果是:
    file3.length=112980
    file2.length=112980
    false谁能告诉我为什么呢?指向的都是同一个文件,打印的文件大小都一样,那为什么2个文件不相等呢?
      

  5.   

    谢楼上,那我的Struts2上传图片后压缩是怎么问题呢
      

  6.   

    问题终于解决了imageext  这里的问题,获取的文件类型为 .jpg  多个"."  把这个去掉就行了  恶心了我好几天  我一直以为是获取文件的问题 哎
      

  7.   

    大哥,能把struts2上传图片进行压缩的源码给我吗?最好是整个项目一下子给我,谢谢
      

  8.   

    struts2 上传后图片后缀是tmp,你的那个工具类支持tmp格式压缩么!?