亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java基于servlet使用組件smartUpload實現(xiàn)文件上傳

 更新時間:2016年10月14日 16:45:09   作者:發(fā)表是最好的記憶  
這篇文章主要介紹了java基于servlet使用組件smartUpload實現(xiàn)文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下

文件上傳在web應用中是非常常見的,現(xiàn)在我就介紹下基于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

下面我們繼續(xù)來看servlet的代碼實現(xiàn):

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的內(nèi)置對象,在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());
  //如果要實現(xiàn)文件的批量上傳,則只需用for循環(huán),將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表單已經(jīng)進行了封裝,這里就不能簡單的用request.getparameter()來獲取表單參數(shù)
  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對文件進行重命名:

在上傳文件等操作中,我們?yōu)榱瞬蛔屛募麤_突,都會進行重命名操作,這里就介紹一個實現(xiàn)IP+時間戳的命名:

直接上代碼了,也沒什么好說的,實現(xiàn)還是挺簡單的,不過實用

package com.xidian.bbs.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class IpTimeStamp {
 private SimpleDateFormat sim=null;//用來獲取時間
 private String ip=null;
 public IpTimeStamp(){
 }
 public IpTimeStamp(String ip){
  this.ip=ip;
 }
 public String getIpTimeRand(){
  StringBuffer sbf=new StringBuffer();
  if(this.ip!=null){
   String a[]=this.ip.split("\\.");    //根據(jù)點來拆分ip地址,但點要轉(zhuǎn)義
   for(int i=0;i<a.length;i++){
    sbf.append(this.addZero(a[i], 3));   //調(diào)用補零的方法,每塊ip不足三位的自動補足到三位
   }
   sbf.append(this.getTimeStamp());    //用this來調(diào)用外部的方法
   Random random=new Random();      //要產(chǎn)生隨機數(shù)
   for(int i=0;i<3;i++){       //產(chǎn)生三位隨機數(shù)
    sbf.append(random.nextInt(10));    //每位隨機數(shù)都不超過10
   }
  }
  return sbf.toString();
 }
 @SuppressWarnings("unused")
 private String getDate(){        //關于日期與時間的實現(xiàn)
  this.sim=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSS");
  return this.sim.format(new Date());
 }
 private String getTimeStamp(){       //返回時間戳
  this.sim=new SimpleDateFormat("yyyymmddhhmmssSSS");
  return this.sim.format(new Date());
 }
 private String addZero(String str,int len){    //自動補零的方法,參數(shù)為指定的字符串和長度
  StringBuffer s=new StringBuffer();
  s.append(str);
  while(s.length()<len){
   s.insert(0,"0");        //在零的位置上進行補零操作
  }
  return s.toString();
 }
 
 //做測試
 public static void main(String [] ary){
  IpTimeStamp IpTimeStamp=new IpTimeStamp("172.168.3.222");//調(diào)用有參數(shù)的構造方法
  System.out.println(IpTimeStamp.getIpTimeRand());
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    這篇文章主要介紹了深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位,結合實例形式詳細分析了Java對象的創(chuàng)建、內(nèi)存布局、訪問定位相關概念、原理、操作技巧與注意事項,需要的朋友可以參考下
    2019-09-09
  • 關于Spring AOP使用時的一些問題匯總

    關于Spring AOP使用時的一些問題匯總

    這篇文章主要給大家匯總介紹了關于Spring AOP使用時的一些問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Java ArrayList如何實現(xiàn)生成不重復隨機數(shù)

    Java ArrayList如何實現(xiàn)生成不重復隨機數(shù)

    這篇文章主要介紹了Java ArrayList如何實現(xiàn)生成不重復隨機數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 快速排序和分治排序介紹

    快速排序和分治排序介紹

    這篇文章主要介紹了快速排序和分治排序,需要的朋友可以參考下
    2015-04-04
  • Java.SE數(shù)組的一些常見練習題

    Java.SE數(shù)組的一些常見練習題

    數(shù)組可以看成是相同類型元素的一個集合,在內(nèi)存中是一段連續(xù)的空間,這篇文章主要給大家介紹了關于Java.SE數(shù)組的一些常見練習題,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?系列產(chǎn)品(IDEA、Pycharm?等)使用本站破解教程?(opens?new?window),在輸入激活碼時,部分小伙伴反應說提示?Key?is?invalid?無法激活,今天小編給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • SpringBoot+Mybatis-plus+shardingsphere實現(xiàn)分庫分表的方案

    SpringBoot+Mybatis-plus+shardingsphere實現(xiàn)分庫分表的方案

    實現(xiàn)億級數(shù)據(jù)量分庫分表的項目是一個挑戰(zhàn)性很高的任務,下面是一個基于Spring Boot的簡單實現(xiàn)方案,感興趣的朋友一起看看吧
    2024-03-03
  • Java8并行流中自定義線程池操作示例

    Java8并行流中自定義線程池操作示例

    這篇文章主要介紹了Java8并行流中自定義線程池操作,結合實例形式分析了并行流的相關概念、定義及自定義線程池的相關操作技巧,需要的朋友可以參考下
    2019-05-05
  • Java基礎之Spring5的核心之一IOC容器

    Java基礎之Spring5的核心之一IOC容器

    這篇文章主要介紹了Java基礎之Spring5的核心之一IOC容器,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Mybatis-Plus處理Mysql?Json類型字段的詳細教程

    Mybatis-Plus處理Mysql?Json類型字段的詳細教程

    這篇文章主要給大家介紹了關于Mybatis-Plus處理Mysql?Json類型字段的詳細教程,Mybatis-Plus可以很方便地處理JSON字段,在實體類中可以使用@JSONField注解來標記JSON字段,同時在mapper.xml中使用json函數(shù)來操作JSON字段,需要的朋友可以參考下
    2024-01-01

最新評論