如果上面的代码看不太懂的话,下面是完整的代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  request.setCharacterEncoding("gbk");
  RequestContext requestContext = new ServletRequestContext(request);
  if(FileUpload.isMultipartContent(requestContext)){
   DiskFileItemFactory factory = new DiskFileItemFactory();
   factory.setRepository(new File("c:/tmp/"));
   ServletFileUpload upload = new ServletFileUpload(factory);
   //upload.setHeaderEncoding("gbk");
   upload.setSizeMax(2000000);
   List items = new ArrayList();
    try {
     items = upload.parseRequest(request);
    } catch (FileUploadException e1) {
     System.out.println("文件上传发生错误" + e1.getMessage());
    }   Iterator it = items.iterator();
  InputStream in=null;
  String name=null,filetype=null;
  int size=0;
   while(it.hasNext()){
    FileItem fileItem = (FileItem) it.next();
    if(fileItem.isFormField()){
     System.out.println(fileItem.getFieldName() + "   " + fileItem.getName() + "   " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk"));
    }else{
     System.out.println(fileItem.getFieldName() + "   " +
        fileItem.getName() + "   " +
        fileItem.isInMemory() + "    " +
        fileItem.getContentType() + "   " +
        fileItem.getSize());     if(fileItem.getName()!=null && fileItem.getSize()!=0){
     in=fileItem.getInputStream();
     size=(int)fileItem.getSize();
     filetype=fileItem.getContentType();
     PreparedStatement pst=null;
     Connection conn=null;
     try{
       conn=image.database.getconnection();
       if(conn!=null)
         System.out.println("connection is ok!!!!");
       String sql="insert into test_img([id],[name],img,filetype) values(?,?,?,?)";
       pst=conn.prepareStatement(sql);
       
       pst.setInt(1,3);
       pst.setString(2,"photoname");
       pst.setBinaryStream(3,in,size);
       pst.setString(4,filetype);
       System.out.println("to here----------");
       int nResult=pst.executeUpdate();
      System.out.println("文件上传成功!!!");
      pst.close();
       conn.close();
       
     }catch(Exception e){
       e.printStackTrace(System.err);
     }
 
     }else{
      System.out.println("文件没有选择 或 文件内容为空");
     }
    }
再次谢过大家