<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cooky - study blog

    Java, Grails, OpenSolaris, Linux, programming, skateboarding...

    用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳

        前幾天搞文件上傳, Google到了Apache 的 commons-fileupload-1.2.jar 上傳組件, 研究了API然后結(jié)合Hibernate實(shí)現(xiàn)了上傳圖片
    并保存到MySQL數(shù)據(jù)庫(kù). 今天重新寫了一遍, 由于不需要保存到數(shù)據(jù)庫(kù)了, 就實(shí)現(xiàn)了保存成文件格式.

        我的這個(gè)FileUpload web工程源文件下載如下 :
    FileUpload.rar . 解壓到當(dāng)前文件夾后直接剪切到 Tomcat 主目錄下的 webapps 
    目錄下, 啟動(dòng) Tomcat 后在瀏覽器輸入 
    http://localhost:8080/FileUpload/upload.html 查看演示.

        我是用Eclipse 開發(fā). 首先, 下載兩個(gè)開發(fā)包: commons-fileupload 和 commons-io(這個(gè)在應(yīng)用commons-fileupload時(shí)會(huì)用到).
    這兩個(gè)開發(fā)包(jar文件) 在我前面提供的FileUpload 工程下的WEB-INF/lib下可以找到, 你也可以到官方下載:
    http://commons.apache.org/downloads/download_fileupload.cgi 下載 commons-fileupload-1.2.jar
    組件包 (直接下載地址:
    http://apache.mirror.phpchina.com/commons/fileupload/binaries/commons-fileupload-1.2-bin.zip),
    http://commons.apache.org/downloads/download_io.cgi 下載 commons-io-1.3.2 組件包(直接下載地址:
     
    http://apache.mirror.phpchina.com/commons/io/binaries/commons-io-1.3.2-bin.zip). 

        啟動(dòng)Eclipse, 新建Web Project, 取名 FileUpload. 然后在WebRoot文件夾(Web根文件夾)下新建兩個(gè)文件夾(New->Folder) :
    ImagesUploaded 和 ImagesUploadTemp, 分別用來(lái)存放上傳文件和上傳時(shí)的臨時(shí)文件,稍后代碼里會(huì)用到.

         在WebRoot 文件夾下新建文件upload.html, 用來(lái)提供上傳用的form. 源代碼如下: 
         右擊src, 新建一個(gè)Servlet: FileUploadServlet, 路徑設(shè)為src/FileUploadServlet . 保存后WebRoot/WEB-INF/web.xml 更新為:

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>FileUploadServlet</servlet-name>
        <servlet-class>FileUploadServlet</servlet-class>
      </servlet>

      <servlet-mapping>
        <servlet-name>FileUploadServlet</servlet-name>
        <url-pattern>/FileUploadServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>upload.html</welcome-file>
      </welcome-file-list>
    </web-app>


       
        其中,我把<welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list> 的歡迎文件設(shè)為了upload.html,
    這樣等完成后可能直接用
    http://localhost:8080/FileUpload/ 來(lái)訪問upload.html. 不改的話, 可以在http://localhost:8080/FileUpload/upload.html
    頁(yè)面上傳文件.    在寫FileUploadServlet 之前, 先將之前下載的commons-fileupload 和commons-io 組件包壓縮文件解壓出來(lái), 把里面的
    commons-fileupload-1.2.jar (lib文件夾下) 和 commons-io-1.3.2.jar 拷到WebRoot/WEB-INF/lib 文件夾下, 以便使用它們的功能.

        FileUploadServlet.java 的源代碼如下, 附有詳細(xì)注解:
    FileUploadServlet.java

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;

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

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;

    public class FileUploadServlet extends HttpServlet {

     public FileUploadServlet() {
      super();
     }

     public void destroy() {
      super.destroy(); // Just puts "destroy" string in log
      // Put your code here
     }

     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      doPost(request, response);
     }

     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      final long MAX_SIZE = 3 * 1024 * 1024;// 設(shè)置上傳文件最大為 3M
      // 允許上傳的文件格式的列表
      final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "txt",
        "doc", "docx", "mp3", "wma", "m4a" };
      response.setContentType("text/html");
      // 設(shè)置字符編碼為UTF-8, 這樣支持漢字顯示
      response.setCharacterEncoding("UTF-8");

      // 實(shí)例化一個(gè)硬盤文件工廠,用來(lái)配置上傳組件ServletFileUpload
      DiskFileItemFactory dfif = new DiskFileItemFactory();
      dfif.setSizeThreshold(4096);// 設(shè)置上傳文件時(shí)用于臨時(shí)存放文件的內(nèi)存大小,這里是4K.多于的部分將臨時(shí)存在硬盤
      dfif.setRepository(new File(request.getRealPath("/")
        + "ImagesUploadTemp"));// 設(shè)置存放臨時(shí)文件的目錄,web根目錄下的ImagesUploadTemp目錄

      // 用以上工廠實(shí)例化上傳組件
      ServletFileUpload sfu = new ServletFileUpload(dfif);
      // 設(shè)置最大上傳尺寸
      sfu.setSizeMax(MAX_SIZE);

      PrintWriter out = response.getWriter();
      // 從request得到 所有 上傳域的列表
      List fileList = null;
      try {
       fileList = sfu.parseRequest(request);
      } catch (FileUploadException e) {// 處理文件尺寸過(guò)大異常
       if (e instanceof SizeLimitExceededException) {
        out.println("文件尺寸超過(guò)規(guī)定大小:" + MAX_SIZE + "字節(jié)<p />");
        out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
        return;
       }
       e.printStackTrace();
      }
      // 沒有文件上傳
      if (fileList == null || fileList.size() == 0) {
       out.println("請(qǐng)選擇上傳文件<p />");
       out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
       return;
      }
      // 得到所有上傳的文件
      Iterator fileItr = fileList.iterator();
      // 循環(huán)處理所有文件
      while (fileItr.hasNext()) {
       FileItem fileItem = null;
       String path = null;
       long size = 0;
       // 得到當(dāng)前文件
       fileItem = (FileItem) fileItr.next();
       // 忽略簡(jiǎn)單form字段而不是上傳域的文件域(<input type="text" />等)
       if (fileItem == null || fileItem.isFormField()) {
        continue;
       }
       // 得到文件的完整路徑
       path = fileItem.getName();
       // 得到文件的大小
       size = fileItem.getSize();
       if ("".equals(path) || size == 0) {
        out.println("請(qǐng)選擇上傳文件<p />");
        out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
        return;
       }

       // 得到去除路徑的文件名
       String t_name = path.substring(path.lastIndexOf("\\") + 1);
       // 得到文件的擴(kuò)展名(無(wú)擴(kuò)展名時(shí)將得到全名)
       String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
       // 拒絕接受規(guī)定文件格式之外的文件類型
       int allowFlag = 0;
       int allowedExtCount = allowedExt.length;
       for (; allowFlag < allowedExtCount; allowFlag++) {
        if (allowedExt[allowFlag].equals(t_ext))
         break;
       }
       if (allowFlag == allowedExtCount) {
        out.println("請(qǐng)上傳以下類型的文件<p />");
        for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
         out.println("*." + allowedExt[allowFlag]
           + "&nbsp;&nbsp;&nbsp;");
        out.println("<p /><a href=\"upload.html\" target=\"_top\">返回</a>");
        return;
       }

       long now = System.currentTimeMillis();
       // 根據(jù)系統(tǒng)時(shí)間生成上傳后保存的文件名
       String prefix = String.valueOf(now);
       // 保存的最終文件完整路徑,保存在web根目錄下的ImagesUploaded目錄下
       String u_name = request.getRealPath("/") + "ImagesUploaded/"
         + prefix + "." + t_ext;
       try {
        // 保存文件
        fileItem.write(new File(u_name));
        out.println("文件上傳成功. 已保存為: " + prefix + "." + t_ext
          + " &nbsp;&nbsp;文件大小: " + size + "字節(jié)<p />");
        out.println("<a href=\"upload.html\" target=\"_top\">繼續(xù)上傳</a>");
       } catch (Exception e) {
        e.printStackTrace();
       }
     }

     }

     public void init() throws ServletException {
      // Put your code here
     }

    }

      

        保存后部署項(xiàng)目(MyEclipse下直接點(diǎn)擊Deploy J2EE...按鈕, 然后選擇Tomcat服務(wù)器). 沒有MyEclipse的話, 把WebRoot 拷到
    Tomcat 主目錄下的webapps 目錄下, 并重命名為 FileUpload 然后啟動(dòng)Tomcat. (或直接下載我在文章開始提供的工程打包rar文件).
    最后打開瀏覽器, 輸入url:
     
    http://localhost:8080/FileUpload/upload.html .

        關(guān)于開發(fā)工具的基本配置( Eclipse 等), 請(qǐng)參加我的另一篇文章.


    Keep It Simple, Stupid

    posted on 2007-10-02 19:55 Cooky 閱讀(25405) 評(píng)論(51)  編輯  收藏 所屬分類: Java

    評(píng)論

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-12 22:16 xpg

    好文章,先試用一下,謝謝??!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-13 01:02 cooky

    @xpg
    3Q!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-18 22:08 lion

    不錯(cuò) 謝謝分享  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-19 00:37 cooky

    @lion
    :)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-20 13:24 cooky

    用Firefox看排版有點(diǎn)小問題...不好意思  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-22 19:23 nike

    just test  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-22 19:24 nike

    確實(shí)很強(qiáng)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-22 19:28 nike

    你很厲害哦  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-10-22 22:45 cooky

    @nike
    .....  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-11-05 23:23 WRF

    謝謝!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-11-18 14:15 上帝也犯困

    頂一下,非常的感謝你.  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)上傳 2007-12-13 15:30 現(xiàn)文件

    upload.html  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2007-12-27 15:49 hehe

    ww  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2007-12-28 17:31 常常

    寫得不錯(cuò)很有開源精神,不像有些人代碼開源還用軟件整理得亂七八糟。  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-01-03 21:23 cynthia

    謝謝,正著急呢。  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-01-04 11:09 lyle

    good good!!!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-01-05 00:39 cc

    我把FileUpload.rar解壓到當(dāng)前文件夾后直接剪切到 Tomcat 主目錄下的 webapps 目錄下, 啟動(dòng) Tomcat 后在瀏覽器輸入 http://localhost:8080/FileUpload/upload.html
    出現(xiàn)錯(cuò)誤如下:
    javax.servlet.ServletException: Error allocating a servlet instance
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
    org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    java.lang.Thread.run(Unknown Source)


    root cause

    java.lang.UnsupportedClassVersionError: Bad version number in .class file
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
    org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    java.lang.Thread.run(Unknown Source)

    這是什么原因?

    麻煩大家?guī)兔Ψ治鲆幌?nbsp; 回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-01-05 12:34 cooky

    java.lang.UnsupportedClassVersionError: Bad version number in .class file . 這個(gè)應(yīng)該是由于JDK版本不一致. 我原來(lái)的好像是用JDK1.6做的. 你需要重新編譯一下. 版本一致有三個(gè)地方需要設(shè)置:
    1.安裝JDK(如1.5或1.6), 設(shè)置JAVA_HOME變量. 在Eclipse的菜單:
    Window->Preferences->Java->Installed JREs: (Add...)jdk1.5.x(或1.6.x)
    2.設(shè)置編譯版本:Window->Preferences->Java->Compiler:Compiler compliance level: 5.0(或6.0, 跟第1步中的一致)
    3.設(shè)置Tomcat運(yùn)行環(huán)境:只要在第1步中設(shè)置了JAVA_HOME環(huán)境變量后,Tomcat會(huì)使用設(shè)置的JDK版本. 在MyEclipse需要另外進(jìn)行設(shè)置:
    Window->Preferences->MyEclipse->Application Servers->Tomcat->Tomcat 6.x(或5.x)->JDK:jdk1.5.x(或1.6.x)
    [先把你要的Tomcat版本Enable一下]

    1,2,3中的選擇的JDK版本要一致.
      回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-01-05 12:35 cooky

    @cc
    如上  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-04-01 09:55 temper

    上傳大文件,比如為幾百m時(shí),開始的時(shí)候會(huì)在fileList = sfu.parseRequest(request);拋出尺寸過(guò)大的異常,但是好像程序會(huì)繼續(xù)上傳,或者進(jìn)行別的操作,幾分鐘以后才會(huì)跳到異常頁(yè)(時(shí)間可能根據(jù)個(gè)人的機(jī)器配置不同)。
    請(qǐng)問有沒有一個(gè)可以遇到異常,馬上到錯(cuò)誤頁(yè)的方法?  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-04-01 10:05 Cooky

    @temper 好像是這樣的,我還沒找到好的方法。。。  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-04-08 20:26 初樹雷

    我的  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-04-10 14:11 paulQuei

    我靠,我擺渡過(guò)來(lái)的  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-04-10 14:19 Cooky

    @paulQuei 呵呵  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-04-16 21:37 藍(lán)色自由

    不錯(cuò),謝謝~~~  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-05-11 20:46 lc

    傳遞輸入地址的Form一定要multipart/Form-data編碼嗎?
    我還想Form中輸入一些別的內(nèi)容,用request.getParameter就找不到啊。  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-05-13 20:39 WEIQ

    你寫的很好啊,非常感謝,我在網(wǎng)上找了很多個(gè),沒有一個(gè)像你寫的那么明了的  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-05-13 21:23 WEIQ

    為什么一定要過(guò)濾掉表單里的文本字段啊,想加點(diǎn)文字描述都不行  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-05-13 22:09 Cooky

    @WEIQ
    那個(gè)好像是ServletFileUpload的parseRequest方法的問題,它會(huì)重新解析request. 至于commons-fileupload可不可以一起處理multipart/form-data和文本,我不清楚,應(yīng)該可以的吧. 你再查下資料好了, 這個(gè)我好久沒搞了.
    ;)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-05-18 08:11 王林

    哪位高人可以提供上傳文件到數(shù)據(jù)庫(kù)的代碼?顯現(xiàn)身手吧  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-05-20 14:27 beenylee

    非常感謝您的無(wú)私奉獻(xiàn)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-05-20 14:31 beenylee

    挺佩服你的,我下載到了common fileuplod包以后,看了API還是不會(huì)用,看完您這篇文章才學(xué)會(huì)使用,我想請(qǐng)教的是,對(duì)于一個(gè)陌生的東西(比如common fileupload),想要正確使用它,有沒有普遍的方法?  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-05-21 14:50 dudu

    那我表單傳過(guò)來(lái)的其他數(shù)據(jù)怎么獲得?用request.getParameter就找不到啊  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2008-06-25 13:08 huang

    寫的太好了!謝謝了!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-11-05 12:32 123

    xiexie  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2008-11-05 19:59 cooky

    好久以前寫的了, 多謝大家的支持.
    那時(shí)是在學(xué)校里, 自由時(shí)間比較多, 現(xiàn)在工作半年了, 大多接觸的是 GWT 和 Ruby on Rails. 不過(guò)最近跟幾個(gè)朋友用 Lightweight JavaEE 搞個(gè) Blog 網(wǎng)站, 到時(shí)會(huì)把源碼公開 (在 Google Code 上), 消息我會(huì)更新在我的 blogpost 里 (見此網(wǎng)站左邊的鏈接), 也盡量記得把項(xiàng)目地址發(fā)在這 :)

    祝大家的程序員生涯一帆風(fēng)順 ;-)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2009-01-07 10:42 java

    實(shí)用,謝了  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2009-03-04 00:47 三棵

    不錯(cuò)
      回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2009-04-06 22:12

    謝謝樓主  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2009-04-06 22:12

    謝謝樓主 很好用  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2009-06-02 23:31 江哥

    ImagesUploaded 和 ImagesUploadTemp沒有看到上傳的內(nèi)容,進(jìn)行測(cè)式的時(shí)候,頁(yè)面顯示成功了,也把上傳的文件大小,時(shí)間打印出來(lái)的,就是在ImagesUploaded 和 ImagesUploadTemp沒有看到上傳的內(nèi)容?這是怎么回事?
    dfif.setRepository(new File(request.getRealPath("/")
    + "ImagesUploadTemp"));
    中的request.getRealPath(有下劃線)是不推薦使作的方法,和這個(gè)有關(guān)嗎?  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2009-06-03 09:39 cooky

    @江哥
    可能是文件路徑的問題. 可以使用以下方法來(lái)獲得保存文件的路徑:
    假設(shè)這個(gè)Servlet的.class文件最終被放在 FileUpload/WEB-INF/classes/文件夾中 (Windows 下的文件路徑分隔符好像應(yīng)該使用反斜杠 \), 而要保存文件的文件夾位于 FileUpload/WEB-INF/ImagesUploaded/, 則用以下代碼修改后替換原來(lái)的保存文件的部分 (fileItem.wirte(new File(u_name)) 那兒):
    String fileName = "a.jpg";// TODO replace with your file name
    /* file path separator: "/" for Unix/Linux, "\\" for Windows */
    final String PATH_SEPARATOR = "/";
    File currFolder = null;
    try {
    currFolder = new File(Main.class.getResource("/").toURI());
    } catch (URISyntaxException e) {
    e.printStackTrace();// TODO log the exception
    }
    if (currFolder != null) {
    String pathToSave = currFolder.getParent() + PATH_SEPARATOR + "ImagesUploaded" + PATH_SEPARATOR;
    pathToSave += fileName;
    // System.out.println(pathToSave);
    fileItem.write(new File(pathToSave));
    } else {
    System.out.println("Can not locate the folder to save file");// TODO
    }  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2009-07-15 11:52 小強(qiáng)

    各種方法非常好,謝謝  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2010-01-30 10:15 hao

    很不錯(cuò),頂?。。。?!  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2010-07-06 09:22 wh

    ding ~~~  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2012-03-26 20:34 jiaoxuekun

    求源碼  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳[未登錄] 2012-09-26 14:31 s

    謝謝~~  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2012-10-19 18:54 ly

    多謝啊 看了這個(gè)學(xué)習(xí)了  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2012-11-26 13:52 tw

    3Q  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2012-12-19 19:20 清泉..

    謝謝,寫得很詳細(xì)  回復(fù)  更多評(píng)論   

    # re: 用commons-fileupload-1.2 實(shí)現(xiàn)文件上傳 2014-08-30 21:30 213213

    非常感謝。已經(jīng)掌握了  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 国产大片免费观看中文字幕| 国产成人AV免费观看| 亚洲欧美日韩中文无线码 | 亚洲成Av人片乱码色午夜| 伊人久久精品亚洲午夜| 中文字幕亚洲无线码a| 在线观看亚洲精品福利片| 黄网站色视频免费观看45分钟 | 国产V亚洲V天堂A无码| 亚洲国产综合无码一区| 亚洲精品乱码久久久久久| 亚洲成a人片在线观看无码专区| 亚洲s色大片在线观看| 亚洲伦另类中文字幕| 成人au免费视频影院| 性xxxx视频播放免费| 国产乱子影视频上线免费观看| 国产jizzjizz免费视频| 亚洲日本一区二区三区在线不卡| 亚洲精品国产日韩无码AV永久免费网| 亚洲精品国产电影| 亚洲av无码一区二区三区乱子伦| 亚洲成熟xxxxx电影| 国产精品亚洲四区在线观看| 亚洲精品无码不卡在线播放| 男人扒开添女人下部免费视频| 国产免费区在线观看十分钟 | 亚洲avav天堂av在线网毛片| 一级做a爱过程免费视| 久久国产精品免费网站| 免费无码黄十八禁网站在线观看| 在线A级毛片无码免费真人| 亚洲国产精品一区二区第一页免| 亚洲中文字幕无码一久久区| 亚洲视频精品在线| 日本亚洲免费无线码 | 精品国产_亚洲人成在线| 中文字幕无线码免费人妻| 亚洲精品在线免费看| 国产无遮挡又黄又爽免费视频 | 国产日本一线在线观看免费|