程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 使用JSP/Servlet上載文件

使用JSP/Servlet上載文件

編輯:關於JSP

         使用 JSP/ Servlet上載文件正成為一項常用的任務。以下是一個簡單的例程,使用了jspsmart的一個免費的組件。你可以在JSPSMART站點進行下載。

    1. Html File
    <html>
    <body>
    <script LANGUAGE="javascript" SRC="JSFunction.js">
    </script>
    <script language="JavaScript">
    <!--
    function checkForm(){

    if (document.uploadform.adsImage.value==""){
    alert("You must choose what image file to upload!");
    return false;
    }
    document.uploadform.submit();
    }
    function cancelUpload(){
    window.close();
    window.opener.focus();
    }
    //-->
    </script>
    <form name="uploadform" method="post"
    action="uploadImage.jsp" ENCTYPE="multipart/form-data"
    target=_self>
    <center>
    <table border="1" width="65%">
    <tr>
    <td colspan=2 nowrap align=center>廣告圖片上載</td>

    </tr>
    <tr>
    <td width="15%" nowrap >廣告圖片:</td>
    <td width="50%" nowrap><input type="file"
    name="adsImage" size="20"></td>
    </tr>
    <tr>
    <td align="center" colspan=2 >
    <input type="button" value="upload" onclick="return
    checkForm();" >
       
    <input type="button" value="Cancel" onclick="return
    cancelUpload();">
    </td>

    </tr>
    </form>
    </table>
    <!--
    <a href="Javascript:window.close();">close this window</a>
    -->
    <script language="JavaScript">
    <!--
    this.focus();
    //-->
    </script>
    </body>

    2. JSP File
    <%@ page language="java" import="com.jspsmart.upload.*,
    java.sql.*,java.util.* "%>
    <%-- use SmartUpload bean --%>
    <jsp:useBean id="mySmartUpload" scope="page"
    class="com.jspsmart.upload.SmartUpload" />

    <%

    //Initialization
    mySmartUpload.init(config);
    mySmartUpload.service(request,response);

    //Set Restriction
    mySmartUpload.setAllowedFilesList("gif,bmp,jpeg,jpg");
    mySmartUpload.setTotalMaxFileSize(51200);



    // Upload
    try{
    mySmartUpload.upload();
    }
    catch(Exception e){
    out.println("<font color=red>Upload File Fail!</font>
    <a href=
    "JavaScript:window.history.back();">back</a><br>");
    out.println("<UI>Notes:</UI>");

    out.println("<UL>You must set correct File
    Name.</UL>");
    out.println("<UL>You file size must be less than
    50K.</UL>");
    out.println("<UL>You can only upload .Gif .jpeg .jpg and
    .bmp files.</UL>");

    return;

    }
    //save file to disk
    mySmartUpload.getFiles().getFile(0).saveAs("/" +
    "FileName");



    %>



    3. Servlet File (you can do it yourself)

     

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved