文件上传解决方案(基于jspsmartupload)

这个程序是苦于jspsmartupload而完成的,大家可以自已去下载jspsmartupload.jar包。

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the uploadfile</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>test.UploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/servlet/UploadServlet</url-pattern>
  </servlet-mapping>
</web-app>
index.jsp文件


<html>
<head>
<title> Jspsmart.html </title>
<meta http-equiv ="Content-Type"  content ="text/html; charset=GB2312">
</head>
<body>
<center>该文件上传共有两种方法,一种是在jsp页面上进行上<br>
传,另一种是用servlet进行上传,只需要改form表单的action即可</center>
<h2> 文件上传范例 - jspSmart </h2>

<form name ="Form1" enctype ="multipart/form-data"  method ="post"  action ="servlet/UploadServlet">
<p> 上传文件 1: <input  type ="file"  name ="File1"  size ="20" maxlength ="20"></p>
<p> 上传文件 2: <input  type ="file"  name ="File2"  size ="20" maxlength ="20" ></p>
<input type ="submit"  value ="上传">
<input type ="reset"alue ="清除">
</form>
<br>
<a href="download.jsp">下载测试</a>
</body>
</html>

jspsmart文件

<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
<title>CH9 - Jspsmart2.jsp</title>
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<jsp:useBean id="mySmartUpload" scope="page" class="SmartUpload"/>
<%
//计算文件上传个数
int count=0;

//SmartUpload的初始化,使用这个jspsmart一定要在一开始就这样声明
mySmartUpload.initialize(pageContext);

//依据form的内容上传
mySmartUpload.upload();

//将上传的文件一个一个取出来处理
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//取出一个文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);

//如果文件存在,则做存档操作
if (!myFile.isMissing()) {

//将文件存放于绝对路径的位置
myFile.saveAs("C:\\upload\\" + myFile.getFileName(),SmartUpload.SAVE_PHYSICAL);

//显示此上传文件的详细信息
out.println("FieldName = " + myFile.getFieldName() + "<BR>");
out.println("Size = " + myFile.getSize() + "<BR>");
out.println("FileName = " + myFile.getFileName() + "<BR>");
out.println("FileExt = " + myFile.getFileExt() + "<BR>");
out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
out.println("ContentType = " + myFile.getContentType() + "<BR>");
out.println("ContentDisp = " + myFile.getContentDisp() +"<BR>");
out.println("TypeMIME = " + myFile.getTypeMIME() +"<BR>");
out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
count ++;
}
}

// 显示应该上传的文件数目
out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>");

// 显示成功上传的文件数目
out.println(count + "file(s) uploaded.");
%>

</body>
</html>

UploadServlet.java文件(这是一个servlet)

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;

public class UploadServlet extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public UploadServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  request.setCharacterEncoding("GBK");
  response.setCharacterEncoding("GBK");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is ");
  out.print(this.getClass());
  out.println(", using the GET method");
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html");
  request.setCharacterEncoding("GBK");
  response.setCharacterEncoding("GBK");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>JspSmartUploadSaveServlet</title></head>");
        out.println("<body bgcolor=\"#ffffff\">");
        saveFile(request, response, out);
        out.println("<p>文件保存完成" + request.getMethod() +".<a href=\"index.html\">继续上传文件</a></p>");
        out.println("</body>");
        out.println("</html>");
        out.close();
 }
 
    private void saveFile(HttpServletRequest request,HttpServletResponse response, PrintWriter out) {
  //上传的文件个数
  int count = 0;
  //文件描述
  String FileDescription[] = {null, null, null};
  SmartUpload mySmartUpload = new SmartUpload();
  
  ServletConfig servletConfig = getServletConfig();
  try {
  //SmartUpload之初始化,一定要初始化才可以使用
  mySmartUpload.initialize(servletConfig, request, response);
  //进行相应处理
  mySmartUpload.upload();
  //最大文件//20mb
  mySmartUpload.setMaxFileSize(20 * 1024 * 1024);
  } catch (Exception e) {
  }
  
  //取得所有文件信息
  File myFile;
  for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
  //产生1个File对象
  myFile = mySmartUpload.getFiles().getFile(i);
  
  //如果文件存在,则把他存入指定位址
  if (!myFile.isMissing()) {
    try { //save file
        myFile.saveAs("c:\\upload\\" + myFile.getFileName(),SmartUpload.SAVE_PHYSICAL);
    } catch (Exception ex) {
        System.out.println(" error---" + ex.getMessage());
    }
  
    out.println("<font color = \"red\">您上传的第" + count +
                "个的文件 :</font><BR> ");
    out.println("文件名称为 :" + myFile.getFileName() + "<BR>");
    out.println("文件类型为 :" + myFile.getContentType() + "<BR>");
    out.println("文件说明为 :" + FileDescription[i] + "<BR>");
    count++;
  
  }
  }

}

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

 

你可能感兴趣的:(html,jsp,xml,Web,servlet)