基於Servlet+smartUpload的檔案上傳

來源:互聯網
上載者:User

標籤:ansi   saveas   protect   操作   parameter   input   exception   時間戳記   htm   

檔案上傳在web應用中是非常常見的,現在我就介紹下基於servlet的檔案上傳,基於Struts2的檔案上傳可以看:

頁面端代碼:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>註冊</title></head><body>          <form name="form1" onsubmit="return on_submit()"                action="RegisterServlet" method="post" enctype="multipart/form-data">                                    <input type="text" name="uname1" id="password" />                                    <input type="text" name="uname2" id="uname2"/>                                    <input type="password" name="password1" id="password" />                                    <input type="password" name="password2" id="password"/>                                    <input type="radio" value="男" checked="checked" name="sex"/>男                                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                         <input type="radio" value="女" name="sex"/>女                                    <input type="text" name="email" value=""  class="box" id="login" />                                    <br/><br/>                                    <input type="file" name="file1" id="file"/>                     <input type="submit" name="submit" value="完成註冊" />          </form></body></html>
 

這裡要注意的一點就是存在檔案上傳的form表單必須封裝為enctype="multipart/form-data";這裡我們直接與後台進行互動,不進行Ajax互動,需要使用ajax可以看:
http://www.cnblogs.com/shenliang123/category/372520.html

 

下面我們繼續來看servlet的代碼實現:

package com.xidian.bbs.servlet;import java.io.IOException;import java.io.PrintWriter;import java.net.InetAddress;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;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.*;import com.xidian.bbs.bean.Bean;import com.xidian.bbs.bean.RegisterBean;import com.xidian.bbs.util.DBAccess;import com.xidian.bbs.util.IpTimeStamp;@SuppressWarnings("serial")public class RegisterServlet extends HttpServlet{    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        response.setCharacterEncoding("GBK");        request.setCharacterEncoding("GBK");                SmartUpload smart=new SmartUpload();        try{        //PageContext是jsp的內建對象,在servlet不能直接使用,需要做一些處理        JspFactory _jspxFactory = null;         PageContext pageContext = null;         _jspxFactory = JspFactory.getDefaultFactory();         pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);        smart.initialize(pageContext);//初始化上傳操作        smart.upload();        IpTimeStamp its=new IpTimeStamp(InetAddress.getLocalHost().getHostAddress());//request.getRemoteAddr()獲得使用者的ip地址        //System.out.println("擷取的ip為"+InetAddress.getLocalHost().getHostAddress());        //如果要實現檔案的批量上傳,則只需用for迴圈,將getFile(0)中的0改為i即可        String ext=smart.getFiles().getFile(0).getFileExt();//此為得到檔案的副檔名,getFile(0)為得到唯一的一個上傳檔案        String fileName=its.getIpTimeRand()+"."+ext;        //System.out.println("擷取 的檔案名稱為"+fileName);        //this.getServletContext().getRealPath("/")為得到tomcat的跟目錄,放於upload檔案夾中,java.io.File.separator是一種安全操作        //String realPath="";        //this.getServletContext().getRealPath("/")+        smart.getFiles().getFile(0).saveAs("/headupload"+java.io.File.separator+fileName);        String realPath="headupload/"+fileName+"";                //        //由於前面的form表單已經進行了封裝,這裡就不能簡單的用request.getparameter()來擷取表單參數        String uname1 = smart.getRequest().getParameter("uname1");//暱稱        String upass1 = smart.getRequest().getParameter("password1");        String sex = smart.getRequest().getParameter("sex");        String uname2 = smart.getRequest().getParameter("uname2");//使用者名稱        String email = smart.getRequest().getParameter("email");        PrintWriter out = response.getWriter();                       //以下是持久層操作,省略。。。。。。。。。。    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {            doGet(request,response);    }}

上面使用到的ip+時間戳記的類IpTimeStamp對檔案進行重新命名:http://www.cnblogs.com/shenliang123/archive/2012/04/19/2456645.html

基於Servlet+smartUpload的檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.