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