檔案上傳(jspsmart實現)file和text表單同時提交的問題

來源:互聯網
上載者:User
 

原來這世界,就是一個圓.

自始至終我都在圓圈裡運動,從起點到起點,從終點到終點.在網上搜了無數的資料,為了尋找text表單和file檔案一起提交的方法,累的腦袋直響.最後回到了開始的地方.找到了那條,曾經被我忽略的代碼.原來,它可以這樣簡單.

好吧,我直切正題,下面的例子中有從網上哪位前輩寫的內容,我只是稍加改動,寫本文沒有商業目的,前輩原諒我沒引入你的大名啊.

程式有一個提交頁面,其實用html就好了,不過原代碼用的是jsp我也拿來用吧.

selectfile.jsp---->web.xml >servletUpload.java  基本就是這麼個結構

下面是代碼:

//selectfile.jsp

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

<html>
<head>
 <title>file upload</title>
</head>

<body>
<font size="5" color="#FF0000">
   <b>檔案上傳 - 使用jspsmart upload組件</b>
</font><br>

<form name="selectfile" enctype="multipart/form-data" method="post" action="servletUpload">
 <p>檔案名稱:
 <input type="file" name="ulfile" size="20" maxlength="80"><br>
 </p>
  <p>上傳路徑:
 <input type="text" name="PATH" size="30" maxlength="50"><br>
 </p>
 <p>附加內容:
 <input type="text" name="other" size="30" maxlength="50"><br>
 </p>
 <p>
 <input type="submit" value="上傳">
 <input type="reset" value="清除">
 </p>
</form>

</body>
</html>

//servletUpload.java 

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;

public class servletUpload extends HttpServlet {
 
 private ServletConfig config;
 /**
 * 初始化Servlet
 */
 final public void init(ServletConfig config) throws ServletException {
  this.config = config;
 }
 
 /**
 * 處理GET請求
 */
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  PrintWriter out = response.getWriter();
  out.println("<HTML>");
  out.println("<BODY BGCOLOR='white'>");
  out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
  out.println("<HR><BR>");
  out.println("The method of the HTML form must be POST.");
  out.println("</BODY>");
  out.println("</HTML>");
 }
 
 /**
 * 響應POST請求
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  PrintWriter out = response.getWriter();
  out.println("<HTML>");
  out.println("<BODY BGCOLOR='white'>");
  out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
  out.println("<HR>");

  // 變數定義
  int count=0;
  SmartUpload mySmartUpload = new SmartUpload();

  try {
   // 初始化
   mySmartUpload.initialize(config,request,response);

   // 上傳
   mySmartUpload.upload();
   com.jspsmart.upload.File f1 = mySmartUpload.getFiles().getFile(0);
   String name = f1.getFileName();
  // System.out.println (name);
  
   

   // 儲存上傳檔案到指定目錄
   // PATH為form表單提交過來的
   count = mySmartUpload.save(mySmartUpload.getRequest().getParameter("PATH"));
   //other為form表單提交過來的
   String other=mySmartUpload.getRequest().getParameter("other"); //這裡可以對other進行處理
   //request.getParameter("PATH");request.gerParameter("other");
   
   // 顯示處理結果
   out.println(count + " file uploaded.");

  } catch (Exception e){
   out.println("Unable to upload the file.<br>");
   out.println("Error : " + e.toString());
  }
  
  out.println("</BODY>");
  out.println("</HTML>");
    }
}

//web.xml的配置如下:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

  <servlet>
    <servlet-name>upload</servlet-name>
    <servlet-class>servletUpload</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>upload</servlet-name>
    <url-pattern>servletUpload</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>selectfile.jsp</welcome-file>
  </welcome-file-list>

</web-app>

需要在WEB-INF\lib中引入jspsmart這個包,上網找一下就有,很多都有的下 www.jspsmart.com 這裡是他的官方網站.把編譯後的class檔案放到WEB-INF\classes下就可以運行了.

這裡面用到了jspsmart提供的mySmartUpload.getRequest().getParameter("other"); 這個東西,由於開始的時候覺得PATH地址沒有必要傳遞就早早的把這條代碼刪掉了,後來就想用request.getParameter("")這個得到資訊,可是總是出錯.在網上找了N多文章,很多人面臨同樣的困難.於是想用邏輯關係把這種情況避免掉.就是用單獨的form上傳用另一個form往資料庫裡錄入.可是錄入的時候又得不到要上傳的檔案名稱,我是想把檔案名稱存到資料庫裡的.如果一定要得的話就得放到session裡去,一想這樣太麻煩,弄不好還容易出bug,要是把臨時資訊放到資料庫裡去,有多人一起操作的話又是個問題,其中還遇到了想往file的屬性value裡寫資訊的問題.只能讀,不能寫,就是這個結果.每次都是快成功的時候就卡在這樣的小地方了.於是上網尋找其他組件看看能不能有相應的功能.這時候使用了fileupload這個組件,網友使用的情況來看這個也要好於jspsmart可是同樣沒找到getParameter這樣的方法.

於是繼續在網上搜,結果找到自己原來用的那段代碼,發現...原來mySmartUpload.getRequest().getParameter就可以實現了.巨汗啊.現在改成這個樣子,可以運行了.不過也許後面還要改成其他的組件,使上傳的資料更穩定一些.現在就先這樣了,商務邏輯已經實現了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.