//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 'index.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>
    <form action="${pageContext.request.contextPath }/Upload3" method="post" enctype="multipart/form-data"> 
      用户名;<input type="text" name="username"/><br/> 
      照 片:<input type="file" name="zhaoPian"/><br/> 
      <input type="submit" value="上传"/> 
    </form> 
  </body>
</html>





//servlet


package com.kero99.ygc.test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;



public class Upload extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置响应浏览器格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String savapath=getServletContext().getRealPath("/WEB-INF/files");
        DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
        ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);
        try {
            fileUpload.setHeaderEncoding("utf-8");
            List<FileItem> list=fileUpload.parseRequest(request);
            for(FileItem item:list){
                if(item.isFormField()){
                    String name=item.getFieldName();//获取文本项
                    String value=item.getString("utf-8");//获取文本内容
                    String value1=new String(name.getBytes("iso8859-1"),"UTF-8");
                }else{//为上传的是文件        
                    String filename1=item.getName();
                    String filename=UUID.randomUUID().toString().replace("-","")+filename1;
                    //目录打散方法 a/b
                    String path=getFileChildPath(filename);        
                    File file=new File(savapath,path);
                    if(!file.exists() && !file.isDirectory()){
                        file.mkdirs();      
                        String extension=FilenameUtils.getExtension(filename);
                        if(!("jsp".equals(extension)||"txt".equals(extension))){
                            File file1=new File(file,filename);
                            item.write(file1);
                            response.getWriter().print("success");
                        }else{
                            response.getWriter().print("文件不能为jsp和exe类型");
                        }
                    }else{
                        response.getWriter().print("文件不能是空的");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //目录打散
    public String getFileChildPath(String filename){
        int hCode=filename.hashCode();
        String hex=Integer.toHexString(hCode);
        String path=hex.charAt(0)+"/"+hex.charAt(1);
        return path;
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
    }

}