<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    利用SmartUpLoad實現文件的上傳

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

    }



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


    網站導航:
     
    主站蜘蛛池模板: 国产三级在线免费| XXX2高清在线观看免费视频| 最近免费视频中文字幕大全| 亚洲Av无码精品色午夜| 久久aⅴ免费观看| 亚洲精彩视频在线观看| 久久久高清免费视频| 亚洲日韩看片无码电影| 日本一道在线日本一道高清不卡免费 | 搜日本一区二区三区免费高清视频 | 中文字字幕在线高清免费电影| 亚洲色无码一区二区三区| a级午夜毛片免费一区二区| 亚洲AV日韩精品久久久久久久 | 亚洲宅男永久在线| 4hu四虎最新免费地址| 亚洲啪AV永久无码精品放毛片| 国产精品色午夜视频免费看| 男女作爱免费网站| 亚洲AV无码久久精品成人| 最近中文字幕大全免费视频| 亚洲一区欧洲一区| www.亚洲精品| 鲁丝片一区二区三区免费| 亚洲av无码不卡久久| 亚洲国产V高清在线观看| 一级毛片在线观看免费| 自拍偷区亚洲国内自拍| 中文字幕在线亚洲精品 | 国产成人aaa在线视频免费观看| 国产黄在线观看免费观看不卡 | 国产亚洲人成在线播放| 亚洲人成网亚洲欧洲无码久久| 久久久久久精品免费看SSS | 亚洲毛片av日韩av无码| 57pao一国产成视频永久免费| 国产精品亚洲专区无码唯爱网| 亚洲精品国偷自产在线| 美女黄网站人色视频免费国产 | 成人看的午夜免费毛片| 在线观看黄片免费入口不卡|