如果是javamail中的得到附件的大小是用part.getSize().
否则好象没有办法直接得到流的大小。

解决方案 »

  1.   

    我想将这个流转换成文件存起来,但用如下的方法:byte[] b = new byte[1000];.....这样只能读取1000个字节,其他就读不了,怎么办?
      

  2.   

    哦,这样啊。看看下面的上传文件的代码,你就知道了:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;public class UpLoadServlet extends HttpServlet {
    private final String CONTENT_TYPE = "text/html;charset=gb2312";
    private final int MACLEN = 2048 * 1024;
    private final int BUFFERSIZE = 1024 * 8;
    private final String tempFileName = "cxj-10.100.0.13";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletOutputStream out = response.getOutputStream();
    String contentTypeStr = request.getContentType();
    int contentLen = request.getContentLength();
    String contentType = request.getContentType();
    String boundary = getBoundary(contentType);
    ServletInputStream in = request.getInputStream();
    FileOutputStream fou = null;
    byte[] b = new byte[BUFFERSIZE];
    int result;
    try {
    result = in.readLine(b,0,b.length); //读取boundary
    result = in.readLine(b,0,b.length); //读取Content-Disposition
    String upLoadFileName = getUpLoadFileName(new String(b,0,result));
    fou = new FileOutputStream(tempFileName);
    result = in.readLine(b,0,b.length); //读取Content-Type;
    result = in.readLine(b,0,b.length); //读取空行;
    int totalRead = 0;
    result = in.readLine(b,0,b.length);
    while((new String(b,0,result)).trim().indexOf(boundary) == -1) {
    totalRead += result;
    fou.write(b,0,result);
    result = in.readLine(b,0,b.length);
    }
    out.println(totalRead);
    fou.close();
    in.close();
    //处理文件
    dealFile(upLoadFileName,totalRead);
    } catch(Exception ex) {
    System.out.print(ex.toString());
    }
    }

    public void destroy(){}

    /*
     * 得到filename
     */
    private String getUpLoadFileName(String line) {
    int split = line.indexOf("filename=");
    String tempFileName = delQuote(line.substring(split + 9,line.length()).trim());
    if(tempFileName.indexOf("\\") != -1) {
    tempFileName = tempFileName.substring(tempFileName.lastIndexOf("\\") + 1,tempFileName.length());
    }
    return tempFileName;
    }

    /*
     * 得到分隔符
     */
    private String getBoundary(String line) {
    return line.substring(line.indexOf("boundary=") + 9,line.length()).trim();
    }

    /*
     * 去除""
     */
    private String delQuote(String line) {
    if(line.indexOf("\"") != -1) {
    line = line.substring(1,(line.length() - 1));
    }
    return line;
    }

    /*
     * 处理文件,把最后的两个字节删除
     */
    private void dealFile(String fileName, int totalRead) throws IOException {
    File file = new File(tempFileName);
    byte[] b = new byte[totalRead - 2];
    RandomAccessFile rafRead = new RandomAccessFile(file,"r");
    rafRead.readFully(b,0,totalRead - 2);
    rafRead.close();
    file.delete();
    RandomAccessFile rafWrite = new RandomAccessFile(fileName,"rw");
    rafWrite.write(b);
    rafWrite.close();
    }}
      

  3.   

    看错了,我是没用ServletInputStream,我这里只是用InputStream,不是在Servlet里面用的!如何用?