經過百般努力,終於用JSP寫出FileUpload上傳小程式

來源:互聯網
上載者:User
    Reference:       http://www.cnblogs.com/xnxqs/archive/2005/07/13/191927.html

經過百般努力,終於可以寫出一個自己的上傳小程式了,其中以上面的url為標準寫的,不過這個程式和新版本的commons-fileupload-1.0,存在不一樣的地方,就是:

新的是: 

void write(java.io.File file)
          A convenience method to write an uploaded item to disk.

而這裡的是String!!!

多虧我還有一點java基礎,,通過尋找api

Constructor Summary

File(File parent, String child)
          Creates a new File instance from a parent abstract pathname and a child pathname string.

 

File(String pathname)
          Creates a new File instance by converting the given pathname string into an abstract pathname.

 

File(String parent, String child)
          Creates a new File instance from a parent pathname string and a child pathname string.

 

File(URI uri)
          Creates a new File instance by converting the given file: URI into an abstract pathname.

 

 

 

我修改了這句為兩句:

File writeFile=new File("F:\\public",name);

 fi.write(writeFile);

 

這樣這個上傳程式終於可以工作了!(一會有原始碼,上傳一個檔案的)

還有這裡的"" 都是""(IME問題),如果粘貼過來要一個個修改,不知道怎麼回事情,網上的代碼總是有一點讓人難以琢磨的錯誤而不能運行!所以網上的東西只能參考理解之後再自己寫!

 


其它過程:

1.      upload
B 端的上傳

1)      upload目錄:

<form  action="getUpload.jsp" enctype="multipart/form-data" method="POST">
這裡oc4j 的目錄和 tomcat 的目錄可是伺服器硬碟上真實存在的任意目錄!

2)      upload method.

請輸入要上傳的檔案:<input type="FILE" name="file"/>

2.      get
S端讀取:
1).request.getInputStream 進行分析
public ServletInputStream getInputStream() throws java.io.IOException

2).Jakarta 通用庫

3.      download

 

temp sql:

create table upload(

name varchar2(16) primary key  not null,

content clob );

commit;

 

server 端接受檔案上傳, 下載commons-fileupload-1.0, http://jakarta.apache.org/commons/fileupload/

 

說明:

Commons是Apache開放原始碼組織中的一個Java子項目,該項目主要涉及一些開發中常用的模組,例如檔案上傳、命令列處理、資料庫連接池、 XML設定檔處理等。這些項目集合了來自世界各地軟體工程師的心血,其效能、穩定性等方面都經受得住實際應用的考驗。有效地利用這些項目將會給開發帶來 顯而易見的效果。Fileupload就是其中用來處理HTTP檔案上傳的子項目。本文主要介紹如何使用Fileupload來處理瀏覽器提交到伺服器的 檔案資訊。

 

PS:一般下載的*.jar 檔案都是拷貝到Tomcat 5.5\common\lib裡面

 

另外, 由於Fileupload子項目同時要用到另外一個項目commons-Beanutils,所以必須下載Beanutils,並將解壓後的檔案commons-beanutils.jar拷貝到{$TOMCAT}/common/lib目錄下

 

Reference: http://www.7880.com/Info/Article-42b729a0.html

我寫的參考原始碼:

//inputupload.jsp

<%@ page contentType="text/html;charset=Big5"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=Big5">
    <title>inputUpload</title>
  </head>
  <body>
  <%
  request.setCharacterEncoding("big5");
  %>
  <form  action="getUpload.jsp" enctype="multipart/form-data" method="POST" >
  請輸入要上傳的檔案:<input type="FILE" name="file"/>
  <input type="submit" value="確定上傳"/>
  </form>
 
  </body>
</html>

//getUpload.jsp

<%@ page contentType="text/html;charset=GBK"%>

<%@ page import="java.util.*"%>

<%@ page import="java.io.*"%>

<%@ page import="org.apache.commons.fileupload.*"%>

<%@ page import="org.apache.commons.beanutils.*"%>

 

 

 

   

getUpload.jsp   

 

 

 

<%

 

  DiskFileUpload dfu = new DiskFileUpload();

 

  // 設定允許使用者上傳檔案大小,單位:位元組

  dfu.setSizeMax(1000000);

  // maximum size that will be stored in memory?

  // 設定最多隻允許在記憶體中儲存的資料,單位:位元組

  dfu.setSizeThreshold(4096);

  // 設定一旦檔案大小超過getSizeThreshold()的值時資料存放在硬碟的目錄

  dfu.setRepositoryPath("f:\\public");

  //開始讀取上傳資訊

  try{

  List fileItems = dfu.parseRequest(request);

 

 

%>

 

 

 

  <%

  // 依次處理每個上傳的檔案

  Iterator i = fileItems.iterator();

 

  String name =null;

  long size=0;

    while (i.hasNext())

  {

  FileItem fi = (FileItem) i.next();

 

  //忽略其他不是檔案域的所有表單資訊

  if (!fi.isFormField()) {

   name = fi.getName();

   size = fi.getSize();

   if((name==null||name.equals("")) && size==0)

   continue;             }

    name=fi.getName();

    size=fi.getSize();

    name = name.replace(':','_');

   name = name.replace('\\','_');

   File writeFile=new File("F:\\public",name);

   fi.write(writeFile);

   }

  

   }catch(FileUploadException fue)

  { fue.printStackTrace();}

  %>

  

 

 

 

***************

經驗:

1.     API 非常重要.比任何參考書都重要!

2.     任何東西只能做為參考,只有自己寫出來的才是自己的!
 

3.      遇到什麼困難,只有專註和堅持不懈,畢竟已經我們站在別人的肩膀上了,別人開始走的路比我們更艱難,所以我們沒有攻不下來的!

我的參考源碼,記住,只能給你參考,也許程式裡有bug,歡迎指教:)

有問題可以一起討論:)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.