国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSPSmart實現文件上傳時file和text表單同時提交的問題

2024-09-05 00:20:16
字體:
來源:轉載
供稿:網友

  原來這世界,就是一個圓。自始至終我都在圓圈里運動,從起點到起點,從終點到終點.在網上搜了無數的資料,為了尋找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就可以實現了.巨汗啊.現在改成這個樣子,可以運行了.不過也許后面還要改成其他的組件,使上傳的數據更穩定一些.現在就先這樣了,商務邏輯已經實現了。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太白县| 右玉县| 凌云县| 荥阳市| 旺苍县| 南丹县| 临沧市| 昌图县| 惠州市| 赣榆县| 红桥区| 靖西县| 青州市| 龙山县| 福建省| 报价| 舞阳县| 翼城县| 安康市| 沂南县| 巴彦淖尔市| 凤凰县| 梅州市| 浦城县| 抚松县| 田东县| 揭西县| 绍兴县| 广元市| 沙坪坝区| 莒南县| 饶阳县| 墨玉县| 黄浦区| 固安县| 吉木乃县| 田阳县| 吉水县| 炎陵县| 永修县| 清远市|