文件上传在web应用中是非常常见的,现在我就介绍下基于servlet的文件上传,基于Struts2的文件上传可以看:
页面端代码:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>注册
这里要注意的一点就是存在文件上传的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对文件进行重命名:
在上传文件等操作中,我们为了不让文件名冲突,都会进行重命名操作,这里就介绍一个实现IP+时间戳的命名:
直接上代码了,也没什么好说的,实现还是挺简单的,不过实用
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("\."); //根据点来拆分ip地址,但点要转义
for(int i=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



