<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
    ?}

    }



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


    網站導航:
     
    主站蜘蛛池模板: 最近免费中文字幕大全免费版视频| a级毛片免费观看在线| 亚洲精品国偷自产在线| 亚洲蜜芽在线精品一区| 日本高清高色视频免费| 亚洲av福利无码无一区二区| 99久久免费国产精品热| 日本免费中文字幕在线看| 亚洲AV无码精品色午夜在线观看| fc2成年免费共享视频网站| 亚洲中文字幕丝袜制服一区| 精品亚洲AV无码一区二区三区 | 亚洲手机中文字幕| 国产亚洲人成在线影院| 最近免费视频中文字幕大全| 久久丫精品国产亚洲av不卡| 91精品视频在线免费观看| 亚洲AV无码一区二区三区在线| 毛片高清视频在线看免费观看| 亚洲老妈激情一区二区三区| 国产麻豆成人传媒免费观看 | 免费无码黄网站在线观看| 日本久久久久亚洲中字幕| 永久免费在线观看视频| 日韩va亚洲va欧洲va国产| 日韩a毛片免费观看| 亚洲线精品一区二区三区| 污视频在线观看免费| 亚洲人成网站在线观看播放青青| 女人18毛片a级毛片免费视频| 亚洲国产成人手机在线电影bd | 亚洲五月午夜免费在线视频| 久操视频免费观看| 亚洲日韩看片无码电影| 亚洲国产aⅴ综合网| 男女猛烈xx00免费视频试看| 亚洲精品午夜国产VA久久成人| 91频在线观看免费大全| 四虎成人精品国产永久免费无码| 香蕉视频在线观看亚洲| 日韩电影免费在线观看视频|