<%@ page language="java" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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="servlet/servletUpload">
<p>文件名称:
<input type="file" name="ulfile" size="20" maxlength="80">
</p>
<p>存储路径:
<input type="text" name="PATH" size="30" maxlength="50">
</p>
<p>
<input type="submit" value="上传">
<input type="reset" value="清除">
</p>
</form>
</body>
</html>
package com.bjsxt;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
public class servletUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
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();
String uploadPath = mySmartUpload.getRequest().getParameter("PATH");
if(uploadPath == null || "".equals(uploadPath)) {
uploadPath = "C:\\apache-tomcat-6.0.16\\webapps\\ServletTest\\upload";
}
try {
// 初始化
mySmartUpload.initialize(config,request,response);
// 上载
mySmartUpload.upload();
// 保存上载文件到指定目录
// PATH为form表单提交过来的
count = mySmartUpload.save(uploadPath);
// 显示处理结果
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>");
}
}