随笔-211  评论-26  文章-8  trackbacks-0
package fileServlet;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileUp extends HttpServlet {
  public static final int MAX_SIZE = 1024 * 1024*100;

  public static final String FILE_DIR = "D:/Temp/";

  public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    DataInputStream dis = new DataInputStream(req.getInputStream());

    PrintStream ps = new PrintStream(res.getOutputStream());
    String content = req.getContentType();
    if (content != null && content.indexOf("multipart/form-data") != -1) {

        int reqSize = req.getContentLength();
        byte[] data = new byte[reqSize];
        int bytesRead = 0;
        int totalBytesRead = 0;
        int sizeCheck = 0;
        while (totalBytesRead < reqSize) {
          // check for maximum file size violation
          sizeCheck = totalBytesRead + dis.available();
          if (sizeCheck > MAX_SIZE) {
            ps.println("文件太大不能上传...");
            return;
          }
          bytesRead = dis.read(data, totalBytesRead, reqSize);
          totalBytesRead += bytesRead;
        }
        String dataString = new String(data, "ISO-8859-1");

        // 分离filepath 并赋值
        dataString = dataString
            .substring(dataString.indexOf("filename=\""));
        String filePath = dataString.substring(0, dataString
            .indexOf("Content-Type:"));

        // 分离contentType 并赋值
        dataString = dataString.substring(dataString
            .indexOf("Content-Type:") + 1);
        // String contentType = dataString.substring(0, dataString.
        // indexOf("\n"));
        dataString = dataString.substring(dataString.indexOf("\n") + 1);
        // 分离文件信息 获得最终想要的字节

        dataString = dataString.substring(2,
            dataString.lastIndexOf("\n") - 1);
        dataString = dataString.substring(0,
            dataString.lastIndexOf("\n") - 1);
        writeFile(dataString.getBytes("ISO-8859-1"), FILE_DIR
            + getFileName(filePath));
        ps.println("文件上传完毕...");
    }

  }

  public boolean writeFile(byte[] data, String path) {
    File f = null;
    FileOutputStream fos = null;
    try {
        f = new File(path);
        f.createNewFile();
        fos = new FileOutputStream(f);
        fos.write(data, 0, data.length);
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        try {
          fos.close();
        } catch (IOException e) {
          return false;
        }
    }
    return true;
  }

  public String getFileName(String arg) {
    String path = arg.substring(arg.indexOf("\"") + 1, arg
          .lastIndexOf("\""));
    path = path.substring(path.lastIndexOf("\\") + 1);
    return path;
  }
}



// 注意:首先得在D:盘下建立Temp目录~!否则回有异常~!经过测试 ,上传10M的东东 ,只需3秒种,如果你的服务器还可以你可以修改public static final int MAX_SIZE = 1024 * 1024*100;的值 上传更大的东东
posted on 2006-08-06 11:24 dragon 阅读(160) 评论(0)  编辑  收藏 所属分类: jsp