锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
聽 <head>
聽聽聽 <base href="<%=basePath%>">
聽聽聽
聽聽聽 <title>My JSP 'upload.jsp' starting page</title>
聽聽聽
聽聽聽 <meta http-equiv="pragma" content="no-cache">
聽聽聽 <meta http-equiv="cache-control" content="no-cache">
聽聽聽 <meta http-equiv="expires" content="0">
聽聽聽 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
聽聽聽 <meta http-equiv="description" content="This is my page">
聽聽聽
聽聽聽 <!--
聽聽聽 <link rel="stylesheet" type="text/css" href="styles.css">
聽聽聽 -->
聽 </head>
聽
聽 <body>
聽聽 <table border="0" align="center" cellpadding="0" cellspacing="0">
聽 <tr>
聽聽聽 <td height="45" align="center" valign="middle">
聽聽聽
聽聽聽 <form action="UpLoad" method="post" enctype="multipart/form-data" name="form1">
聽聽璇烽夋嫨涓婁紶鐨勫浘鐗?br />聽聽聽 <input type="file" name="file">
<input type="submit" name="Submit" value="涓婁紶">
聽聽聽 </form></td>
聽 </tr>
</table>
聽 </body>
</html>
2.servlet綾?鍦╯ervlet閲岀洿鎺ヨ繘琛屼笂浼犳搷浣?
package com.zyc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.SmartUpload;
public class UpLoad extends HttpServlet {
聽/**
聽 * Constructor of the object.
聽 */
聽public UpLoad() {
聽聽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");
聽}
聽/**
聽 * 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");
聽聽
聽聽SmartUpload mySmartUpload =new SmartUpload();
聽聽long file_size_max=4000000;
聽聽String fileName2="",ext="",testvar="";
聽聽String url="images/";
聽聽 try{
聽聽聽聽聽聽聽 JspFactory _jspxFactory = null;
聽聽聽聽聽聽聽 PageContext pageContext = null;
聽聽聽聽聽聽聽 _jspxFactory = JspFactory.getDefaultFactory();
聽聽聽聽聽聽聽 pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);
聽聽
聽聽聽聽聽聽聽 mySmartUpload.initialize(pageContext);
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 mySmartUpload.setAllowedFilesList("jpg,gif");
//聽聽聽聽聽 涓婅澆鏂囦歡
聽聽聽聽聽聽 mySmartUpload.upload();
聽聽聽聽聽聽
聽聽聽聽聽聽 com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
聽聽聽聽聽聽
聽聽聽聽聽聽 String myFileName=myFile.getFileName(); //鍙栧緱涓婅澆鐨勬枃浠剁殑鏂囦歡鍚?br />聽聽聽聽聽聽 ext= myFile.getFileExt();
聽聽聽聽聽聽 int file_size=myFile.getSize();
聽聽聽聽聽聽 String saveurl="";
聽聽聽聽聽聽 Calendar calendar = Calendar.getInstance();
聽聽聽聽聽聽 String filename = String.valueOf(calendar.getTimeInMillis());
聽聽聽聽聽聽 saveurl=request.getRealPath("/")+url;
聽聽聽聽聽聽 saveurl+=filename+"."+ext;
聽聽聽聽聽聽 myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
聽聽聽聽聽聽
聽聽 }catch(Exception e){
聽聽聽 e.printStackTrace();
聽聽 }
聽}
聽/**
聽 * Initialization of the servlet. <br>
聽 *
聽 * @throws ServletException if an error occure
聽 */
聽public void init() throws ServletException {
聽聽// Put your code here
聽}
}