求教:-----struts的---  动态多文件上传,前台通过脚本获得文件内容,动态添加<input type="hidden" name="fileContent">标签。将文件内容赋到value中。 该页面form对应的formbean中定义了一个string类型的数组String[] fileContent   用来存取前台jsp页面 input标签 传过来的值。
该页面对应得Action.java 中,通过String[] fileCnt= batchform.getFileContent();  获取从FormBean中取出传过来的文件内容(string类型的)。也就是说现在的 数组fileCnt 中存放的是各个文件的文件内容。 下面问题来了。流读写操作,小弟不知道如何解决。目前做法是
BufferedReader in =new BufferedReader(new StringReader(fileCnt[i])); 形成流
PrintWriter out =new PrintWriter(new BufferedWriter(new FileWriter(dir+"/"+fileNm[i])));  写流到文件
int lineCount = 1;
 while((s = in.readLine()) != null)
     out.println(lineCount++ + ":" + s);
 out.close();
 in.close();
 可是当文件内容大的情况下,接近1M左右时,就是不好使。也就是说写流到文件这一块有问题。请教各位老师给帮忙解决一下。非常感谢。 QQ 315780342

解决方案 »

  1.   

    不要一次性读取,缓冲下,
    参考:buffer = new byte[1024];
    String path = request.getSession().getServletContext().getRealPath(
    "/images/nophoto.gif");
    File file = new File(path);
    try {
    is = new FileInputStream(file);
    if (is.available() > 1024) {
    int remain;
    while ((remain = is.read()) != -1) {
    out.write(remain);
    }
    } else {
    is.read(buffer);
    out.write(buffer);
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
      

  2.   

    BufferedReader br = new BufferedReader(new StringReader(fileCnt[i]));
    FileWriter fw = new FileWriter(dir+"/"+fileNm[i]);
    char[] chars= new char[2048];
    int s = 0;
    int success = 0;
    while ((s = br.read(chars)) != -1) {
    fw.write(chars, 0, s);
    success++;
    }
    if (success == 0) {
    throw new Exception("错误!");
    }
      

  3.   

    参考下这个
    http://hi.baidu.com/flymz/blog/item/c0fa35fa99b721899f514613.html