}
catch(SQLException ex){
System.err.pintln(ex.getMessage());
}
return result;
}
上传页面如下图4-3所示:
图4-3
上传页面主要代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import java.util.Enumeration;
public class UploadServlet extends HttpServlet {
private String webTempPath;
//单个文件大小限制为1GBytes
private final int maxPostSize = 1024* 1024 * 1024 ;
public void init(){
//指定上传文件的存储目录为Web应用目录下的uploaded子目录
webTempPath = getServletContext().getRealPath("/") + "uploaded";
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// 声明上传文件名所使用的编码,默认值为 ISO-8859-1,
// 若改为GB2312则支持中文名
String enCoding = "GB2312";
// 产生一个新的MultipartRequest 对象,multi
MultipartRequest mpr = new MultipartRequest(request, webTempPath, maxPostSize, enCoding);
Enumeration fileenum = mpr.getFileNames();
Enumeration filesdc = mpr.getParameterNames();
response.setContentType("text/html");
//支持中文输出内容
response.setCharacterEncoding("GB2312");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>文件上传处理</title>");
out.println("</head>");
out.println("<body>");
for (int i = 1; fileenum.hasMoreElements();i++){
//out.println("The name of uploaded file " + i +" is: " + mpr.getFilesystemName((String) fileenum.nextElement()) + "<br><br>");
String name = (String)fileenum.nextElement();
String dc = (String)filesdc.nextElement();
String FileName = mpr.getFilesystemName(name);
String ContentType = mpr.getContentType(name);
String Description = mpr.getParameter(dc);
if (FileName != null)
out.println("<font color='red'>您上传的第" + i +
"个文件:</font><br>"+
"文件名称为:" + FileName +
"<br>文件类型为:" + ContentType +
"<br>文件的描述:" + Description +
"<br><br>");
}
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
throw new ServletException("GET method used with " + getClass().getName()+": POST method required.");
}
}
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] ... 下一页 >>