本人通过struts2上传到tomcat服务器文件打不开,查看时发现比源文件小了,
这是个什么情况,有木有知道的?

解决方案 »

  1.   

    上传过程中出现了流中断现象,一般上传文件在实际应用中都使用servlet,struts2上传有BUG,就是流中断,当流中断的时候,struts2将报错,会一直寻找input返回.所以建议楼主使用servlet上传.前台使用swfupload组件.这是目前的主流做法.
      

  2.   

    关键我这项目是要用SSH来做,甘宁大哥怎么办哪?
      

  3.   

    public class EmailWriteAction extends ActionSupport{
    private int id;
    private String title;
    private String context;
    private File file;//与jsp表单中的名称对应
    private String fileFileName;//FileName为固定格式
    private String fileContentType;//ContentType为因定格式
    //getter,setter方法省略//获取上传文件名,并将文件上传到服务器中
    public String getFilename(){
    System.out.println(fileFileName);
    String root=ServletActionContext.getServletContext().getRealPath("/upload");
    File desfile=new File(root,fileFileName);//文件上传到服务器上,在eclipse目录上查看不到
    //File desfile=new File("E:\\JAVA\\JAVA学习\\单元项目",this.getFileFileName());
    FileUtils.copyfile(file, desfile);
    return fileFileName;
    }//这是我自己编写的copyfile类.没有调用commons-fileupload包里的类.
    public class FileUtils {

    private  static BufferedReader br=null;
    private  static BufferedWriter bw=null;


    public static void copyfile(File sourcefile,File desfile) 
    {
    try {
    br=new BufferedReader(new InputStreamReader(new FileInputStream(sourcefile)));
    bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desfile)));
    String str=null;
    while((str=br.readLine())!=null){
    bw.write(str);
    }
    bw.flush();
    bw.close();
    br.close();
    } catch (FileNotFoundException e1) {
    System.out.println("找不到文件");
    e1.printStackTrace();
    }
    catch (IOException e) {
    System.out.println("文件复制失败");
    e.printStackTrace();
    }
    }
    }表示层应该没有问题,你帮我看一下吧
      

  4.   

    struts2的流就是一堆垃圾
    我很鄙视,所以从来不用
      

  5.   

    fileFileName不用赋值,与file对应,只要上传了file就有相应的fileFileName,这个是在fileupload类就写好的.为什么会出现图片打不开的情况,可能是我用字符流读的原因吧,不知道是不是?
      

  6.   

    终于知道原因了,就是我自己编写的fileutils类有问题,应该用字节流来读取文件,不应该是字符流,经过测试确实是这个问题.