posts - 13, comments - 7, trackbacks - 0, articles - 0

          利用SmartUpLoad實現文件的上傳

          Posted on 2007-01-31 13:34 趙永超 閱讀(669) 評論(0)  編輯  收藏 所屬分類: Jsp

          1.jsp文件

          <%@ 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">
          ??請選擇上傳的圖片
          ??? <input type="file" name="file">
          <input type="submit" name="Submit" value="上傳">
          ??? </form></td>
          ? </tr>
          </table>
          ? </body>
          </html>


          2.servlet類(在servlet里直接進行上傳操作)

          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(); //取得上載的文件的文件名
          ?????? 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
          ?}

          }



          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 佳木斯市| 乌苏市| 林口县| 合阳县| 乡宁县| 平武县| 天镇县| 青田县| 彭山县| 峨山| 沧州市| 固镇县| 万年县| 安顺市| 遂溪县| 西和县| 昭觉县| 绥阳县| 胶州市| 宁国市| 翁源县| 湖南省| 佛冈县| 梓潼县| 泗洪县| 同仁县| 汤阴县| 土默特左旗| 九江市| 出国| 金门县| 堆龙德庆县| 临桂县| 卢龙县| 福建省| 南部县| 那曲县| 彰化市| 通河县| 杨浦区| 光泽县|