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

          利用SmartUpLoad實現(xiàn)文件的上傳

          Posted on 2007-01-31 13:34 趙永超 閱讀(668) 評論(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
          ?}

          }



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


          網站導航:
           
          主站蜘蛛池模板: 大丰市| 福清市| 武定县| 安陆市| 衡水市| 东明县| 丹巴县| 渑池县| 吉安县| 牙克石市| 扎赉特旗| 左权县| 墨脱县| 祁东县| 西城区| 镇平县| 山丹县| 沂南县| 民勤县| 临湘市| 扎兰屯市| 资中县| 五家渠市| 沭阳县| 湘乡市| 旺苍县| 社会| 德兴市| 尉氏县| 长沙县| 建始县| 宜州市| 衡山县| 资兴市| 德化县| 宣化县| 东源县| 章丘市| 桃园县| 小金县| 邮箱|