⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadfile.java

📁 一个学习程序
💻 JAVA
字号:
package WebMail;
/**
 * <p>Title: 企业邮箱管理系统</p>
 * <p>Description: 收发邮件_上传文件</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: 南京仕邦有限公司</p>
 * @author W.L.B
 * @version 1.0  2003 06 18 First Release
 */

import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class UploadFile {
public String[] sourcefile = new String[255];    //源文件名
public String objectpath = "d:/";                //目标文件目录
public String[] objectfilename = new String[255];//目标文件名
public String[] tempfilename   = new String[255];//目标临时文件名
public ServletInputStream sis = null;            //输入流
public String[] description = new String[255];   //描述状态
public long size = 8*1024*1000;                  //限制大小8M
private int count = 0;                           //已传输文件数目
private int filesize  = 0;
private byte[] b = new byte[4096];               //字节流存放数组
private boolean successful = true;

public UploadFile()
  {
   //可以在构造函数里构建服务器上传目录,也可以在javabean调用的时候自己构建
   setObjectpath("d:/");
  }

public void setSourcefile(HttpServletRequest request) throws java.io.IOException
  {
    sis = request.getInputStream();
    filesize = request.getContentLength();

    if (filesize > size)
      {
        description[count] = "ERR The file "+sourcefile[count]+" is too large to transfer. The whole process is interrupted.";
        System.out.println("Length : <" + filesize +" > The file is too large to transfer.");
        successful = false;
        return;
      }

      for (int i =0; i<4;i++)
      {
        sourcefile[i]   = "";
        tempfilename[i] = "";
      }

      int a = 0;  int k = 0; String s = "";
      while((a = sis.readLine(b,0,b.length)) != -1)
      {
        s = new String(b,0,a);
        if((k = s.indexOf("filename=")) != -1)
        {
          s = s.substring(k + 10);
          k = s.indexOf("\"");
          s = s.substring(0, k);
          sourcefile[count] = s;

          s = s.substring(s.lastIndexOf("\\")+1 );
          objectfilename[count] = s;
          if (!s.equals(""))
          {
            System.out.println("上传文件名 :" + sourcefile[count]);
            System.out.println("...");
            transferfile(count);
          }
        }
        if(!successful) break;
      }
  }

  public int getCount()
  {
    return count;
  }

  public String[] getSourcefile()
  {
    return sourcefile;
  }

  public String[] gettmpfile()
  {
    return tempfilename;
  }


  public String getObjectpath()
  {
    return objectpath;
  }

  public void setObjectpath(String objectpath)
  {
    this.objectpath = objectpath;
  }

  public void setSize(long l)
  {
    this.size = l ;
  }


  private void transferfile(int i)
  {
    System.out.println("开始上传...");
    System.out.println("..."  );
    try
    {
      tempfilename[i] = objectpath+objectfilename[i] ;
      FileOutputStream out = new FileOutputStream(objectpath+objectfilename[i]);
      System.out.println("保存的文件名: "+objectpath+objectfilename[i]);
      int a = 0;
      int k = 0;
      long hastransfered = 0;  //标示已经传输的字节数
      String s = "";
      while((a = sis.readLine(b,0,b.length)) != -1)
      {
        s = new String(b,0,a);
        if((k = s.indexOf("Content-Type:")) != -1)
          break;
      }

      sis.readLine(b,0,b.length);
      while((a = sis.readLine(b,0,b.length)) != -1)
      {
        s = new String(b,0,a);
        if((b[0]==45)&&(b[1]==45)&&(b[2]==45)&&(b[3]==45)&&(b[4]==45))
          break;
        out.write(b,0,a);
        hastransfered += a;

        if(hastransfered >= size)
        {
          description[count] = "ERR : "+sourcefile[count]+" is too large to transfer. 将终止 ";
          System.out.println("The file is too large to transfer.");
          successful = false;
          break;
        }
      }
      System.out.println("Transfered Byte is   "+hastransfered+" byte ");
      if(successful)
        description[count] = "The file "+sourcefile[count]+" has been transfered successfully.";
      ++count;
      out.close();
      if(!successful)
      {
        sis.close();
        File tmp = new File(objectpath+objectfilename[count-1]);
        tmp.delete();
      }
    }
    catch(IOException ioe)
    {
      description[i]=ioe.toString();
    }
  }

  public void deleteFile(String str)
  {
    File ff = new File(str);
    ff.delete();
  }


// END
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -