JSP实现文件上传

作者:网络 来源:佚名 更新时间:2008-07-07 12:04:32 点击:
国内最大的酷站演示中心!

搞了几天,在网上查了不少资料,终于把上传功能解决了。程序如下:

package linkman.helper;

import java.io.*;
import java.util.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.servletinputstream;

public class uploadbean {
 private string savepath = null;
 private string filename = null;
 private string sperator = null;
 private string errmsg = "正常返回";
 
 private byte[] bt = new byte[4096];
 private byte[] prebt = new byte[4096];
 private int t = -1;
 private long filesize = -1;
 private boolean userdefinefilename = false;
 
 /////////////////////////////////////////////////
 public uploadbean () {
  filename = bringfilename ();
 }
 public uploadbean (string savepath) {
  savepath = savepath;
  filename = bringfilename ();
 }
 
 /////////////////////////////////////////////////
 // 文件名信息
 private string bringfilename () {
  calendar dt = calendar.getinstance ();
  string str = "" + dt.get (calendar.year) + dt.get (calendar.month) + dt.get (calendar.day_of_month);
  str = str + dt.get (calendar.hour) + dt.get (calendar.minute) + dt.get (calendar.second);
  return str;
 }
 
 public void setfilename (string filename) {
  filename = filename;
  userdefinefilename = true;
 }
 public string getfilename () {
  return filename;
 }
 /////////////////////////////////////////////////
 // 上传文件主程序
 public boolean upload (httpservletrequest request) throws exception {
  if (savepath == null) {
   seterrormessage ("未设置保存上传文件的路径");
   return false;
  }
  request.setcharacterencoding("utf-8");
  servletinputstream in = request.getinputstream ();
  ////////////////////////////////////////////
  // 获取得一行的分隔标志
  t = in.readline (bt, 0, bt.length);
  if (t != -1) {
   sperator = new string (bt, 0, t);
   sperator = sperator.substring (0, 28);
   t = -1;
  }
 
  ////////////////////////////////////////////
  // 取扩展名并设定文件全名
  do {
   t = in.readline (bt, 0, bt.length);
   string s = new string (bt, 0, t);
   int index = s.indexof ("filename=\"");
   if (index != -1) {
    s = s.substring (index + 10);
    index = s.indexof ("\"");
    s = s.substring (0, index);
    index = s.lastindexof (".");
    if (!userdefinefilename) {
     filename = filename + s.substring (index); // 设定文件全名
    }
    t = -1;
   }
  } while (t != -1);
 
  ////////////////////////////////////////////
  // 取文件内容
  fileoutputstream out = new fileoutputstream (savepath + "\\" + filename);
 
  t = in.readline (bt, 0, bt.length);
  string s = new string (bt, 0, t);
  int i = s.indexof ("content-type:");
  if (i == -1) {
   seterrormessage ("上传的不是文件");
   return false;
  } else {
   in.readline (bt, 0, bt.length); // 去掉一个空行
   t = -1;
  }
 
  boolean state = true;
  long trancsize = 0;
  try {
   t = in.readline (bt, 0, bt.length);
   while (t != -1) {
    s = new string (bt, 0, t);
    if (s.length () > 28) {
     s = s.substring (0, 28);
     if (s.equals (sperator)) {
      break;
     }
    }
   
    /*
     if (bt[0] == 45 && bt[1] == 45 && bt[2] == 45 && bt[3] == 45 && bt[4] == 45) {
      break;
     }
    */
    if (filesize != -1) {
     if (trancsize >= filesize) {
      throw new exception ("上传文件太大");
     }
    }
    out.write (bt, 0, t);
    trancsize += t;
    t = in.readline (bt, 0, bt.length);
   }
  } catch (exception e) {
   state = false;
   out.close ();
   seterrormessage (e.getmessage ());
   file tmp = new file (savepath, filename);
   if (!tmp.delete()) {
    seterrormessage (e.getmessage () + ",清除未完成");
   }
  } finally {
   if (state) {
    out.close ();
   }
   return state;
  }
 }
 /////////////////////////////////////////////////
 // 上传文件错误信息
 public void seterrormessage (string errormessage) {
  errmsg = errormessage;
 }
 public string geterrormessage () {
  return errmsg;
 }
 /////////////////////////////////////////////////
 // 文件大小信息
 public void setfilesize (long size) {
  filesize = size;
 }
 
 public long getfilesize () {
  return filesize;
 }
}// end class