網上想找現成的,可是找不到,沒得法只好(修改源程序)自己動手做了一個。
在自己機子上測試成功,可以支持中文。
下載:
/Files/hijackwust/jsmartcom_zh_CN.rar
-----------------------------------------------
沒想到下載的人會這么多。
我把測試代碼發出來,簡單三個文件。
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 action="uploadfile" method="post" enctype="multipart/form-data">
<p>文件名稱: <input type="file" name="file1" size="20" maxlength="80">
</p>
<p>文件名稱: <input type="file" name="file2" size="20" maxlength="80">
</p>
<p>文件名稱: <input type="file" name="file3" size="20" maxlength="80">
</p>
<p>上傳路徑: <input type="text" name="path" size="30" maxlength="50"><br>
</p>
<p>附加內容: <input type="text" name="other" size="30" maxlength="50">
</p>
<p><input type="submit" value="上傳"> <input type="reset"
value="重置"></p>
</form>
<font size=5 color=#FF0000> <b>文件下載----使用jspsmart upload組件</b> </font>
<br>
<form action="downloadfile" method="post">
<p>下載文件的名稱: <input type="text" name="downloadFileName" size="20"
maxlength="80"></p>
<input type="submit" value="下載">
</body>
</form>
</html>
兩個Servlet: ServletUpload 和ServletDownload
public class ServletUpload extends HttpServlet {
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
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();
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myfile = mySmartUpload.getFiles().getFile(i);
String fileName = myfile.getFileName();
count = mySmartUpload.save("/upload");
//count = mySmartUpload.save(null);
}
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>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
public class ServletDownload extends HttpServlet {
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String temp_p =request.getParameter("downloadFileName");
byte[] temp_t=temp_p.getBytes("ISO8859_1");
String fileName=new String(temp_t,"GBK");
SmartUpload mySmartUpload = new SmartUpload();
try {
mySmartUpload.initialize(config, request, response);
mySmartUpload.setContentDisposition(null);
mySmartUpload.downloadFile("/upload/"+fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}