熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

文件上傳的完全攻略

2022-06-13   來源: Javascript 

  . 你在html頁面中寫下面的語句就可以把文件數據傳到servlet中
  <%@page contentType="text/html;charset=gb2312"%>
  
  
  
  
  
  
  
  
  
  . 在sevelt的代碼這樣寫的它調用FileLoad這個類來做文件上傳
    package lh;
  
  /**
  * 此處插入類型描述
  * 創建日期( ::)
  * @author無是無非
  */
  import javaxservlethttp*;
  import javaxservlet*;
  import javautil*;
  import javaio*;
  import javaxservletServletInputStream;
  public class FLServlet extends javaxservlethttpHttpServlet {
  
  /**
  * FLServlet 構造子注解
  */
  public FLServlet() {
  super();
  }
  
  protected void doGet(
    javaxservlethttpHttpServletRequest request
    javaxservlethttpHttpServletResponse response)
    throws javaxservletServletException javaioIOException {
    doPost(request response);
  
  }
  
  protected void doPost(HttpServletRequest request HttpServletResponse response)
    throws javaxservletServletException javaioIOException {
  
    //上傳文件
  
    FileLoad fl = new FileLoad();
    flprepareForFile(request);
  
  }
  }
  
   FileLoad類的代碼如下
  package lh;
  
  /**
  * 此處插入類型描述
  * 創建日期( ::)
  * @author無是無非
  */
  
  import javaxservlethttp*;
  import javaxservlet*;
  import javautil*;
  import javaio*;
  import javaxservletServletInputStream;
  public class FileLoad {
  
    public ServletInputStream sis = null;
    public byte[] b = new byte[]; //字節流存放數組
    public String sourcefile = ; //源文件名
    private int count = ; //已傳輸文件數目
    public double size = DoubleMAX_VALUE; //限制大小
    public String suffix = ; //文件後綴名
    public String objectfilename = ; //目標文件名
    public String objectpath = e:\\; //目標文件目錄
    public boolean successful = true;
  
  /**
  * FileLoad 構造子注解
  */
  public FileLoad() {
  super();
  }
  /**
  * 此處插入方法描述
  * 創建日期( ::)
  * @param response javaxservlethttpHttpServletResponse
  */
  public void prepareForFile(HttpServletRequest request) {
    try {
      sis = requestgetInputStream();
      int a = ;
      int k = ;
      String s = ;
      while ((a = sisreadLine(b blength)) != )
  //判斷是否從文件中讀到內容
        {
        s = new String(b a);
        if ((k = sindexOf(filename=)) != )
  //傳過來的每個文件中都有filename=這段文字
          {
          s = ssubstring(k + );
          k = sindexOf(\);
          s = ssubstring( k); //得到了文件全部名稱
          sourcefile = s;
          k = slastIndexOf();
          suffix = ssubstring(k + ); //得到了文件的擴展名
          transferFile();
        }
  
      }
    } catch (Exception ex) {
      exprintStackTrace();
    }
  }
  /**
  * 此處插入方法描述
  * 創建日期( ::)
  * @param i int
  */
  public void transferFile() {
    String x = LongtoString(new javautilDate()getTime());
    try {
      objectfilename = x + + suffix;
      FileOutputStream out = new FileOutputStream(objectpath + objectfilename);
  
      int a = ;
      int k = ;
      long hastransfered = ; //標示已經傳輸的字節數
      String s = ;
      while ((a = sisreadLine(b blength)) != ) {
        s = new String(b a);
        if ((k = sindexOf(ContentType:)) != )
          break;
      }
      sisreadLine(b blength);
      while ((a = sisreadLine(b blength)) != ) {
        s = new String(b a);
        if ((b[] == )
          && (b[] == )
          && (b[] == )
          && (b[] == )
          && (b[] == ))
          break;
        outwrite(b a);
        hastransfered += a;
        if (hastransfered >= ) {
  
          Systemoutprintln(文件超大了!!);
          successful = false;
          break;
        }
        ++count;
  
      }
      if (!successful) {
        //如果傳送文件沒有成功就刪除掉這個文件
        outclose();
        sisclose();
        String tem = objectpath + objectfilename;
        File del = new File(tem);
        deldelete();
      }
      outclose();
    } catch (IOException ioe) {
      Systemoutprintln(IOException!!);
    }
  }
  }

From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/11138.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.