Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336854
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-01-28 02:54:47

进行文件上传时,必须将表单的method属性设为post,将enctype属性设为multipart/form-data

fileUpload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
 
 



username :

file :

 

 

 

fileUploadResult.jsp

<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="gbk"%>
 
    <%
        InputStream is = request.getInputStream() ;
        BufferedReader br = new BufferedReader(new InputStreamReader(is)) ;
        String buffer = null ;
        while(null != (buffer = br.readLine()))
        {
            out.print(buffer+"
" ) ;
        }
        br.close() ;
        is.close() ;
     %>

 

(注意

import="java.util.*,java.io.*"

)

 

 

Servlet实现文件上传:

fileUpload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
 
 



username :

file :
file2:

 

 

 

fileUploadResult.jsp

<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="gbk"%>
 
     username :${requestScope.username}

     file1:${requestScope.file1}

      file2:${requestScope.file2}

 

 

 

UploadServlet.java

package com.shengsiyuan.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

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;

public class UploadServlet extends HttpServlet
{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        this.doPost(request, response) ;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {DiskFileItemFactory factory = new DiskFileItemFactory() ;
    String path = request.getRealPath("/upload") ;
    factory.setRepository(new File(path)) ;
    factory.setSizeThreshold(1024 *1024) ;
    ServletFileUpload upload = new ServletFileUpload(factory) ;
    try
    {
        List list = (List)upload.parseRequest(request) ;
        for(FileItem item:list)
        {
            String name = item.getFieldName() ;

                if(item.isFormField())
                {
                    String value =     item.getString() ;
                    System.out.println(name +"=" + value );
                    request.setAttribute(name, value) ;
                }else
                {
                    String value = item.getName() ;
                    int start = value.lastIndexOf("\\") ;
                    String fileName = value.substring(start + 1) ;
                    request.setAttribute(name,fileName) ;
                    item.write(new File(path,fileName)) ;
                }
        }
    }catch(Exception ex)
    {
        ex.printStackTrace() ;
    }
    request.getRequestDispatcher("fileUploadResult.jsp").forward(request, response) ;

    }

}

 

需要用到fileupload.jar和io.jar

1

以下为修改后的servlet,手工实现write

package com.shengsiyuan.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

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;

public class UploadServlet extends HttpServlet
{

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
 {
  
  this.doPost(request, response) ;
  
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
 {DiskFileItemFactory factory = new DiskFileItemFactory() ;
 String path = request.getRealPath("/upload") ;
 factory.setRepository(new File(path)) ;
 factory.setSizeThreshold(1024 *1024) ;
 
 ServletFileUpload upload = new ServletFileUpload(factory) ;
 try
 {
  
  List list = (List)upload.parseRequest(request) ;
  for(FileItem item:list)
  {
   String name = item.getFieldName() ;

    if(item.isFormField())
    {
     String value =  item.getString() ;
     System.out.println(name +"=" + value );
     
     request.setAttribute(name, value) ;
     
     
    }else
    {
     String value = item.getName() ;
     int start = value.lastIndexOf("\\") ;
     String fileName = value.substring(start + 1) ;
     
     request.setAttribute(name,fileName) ;
     //item.write(new File(path,fileName)) ;
     //以下用手工方法实现
     OutputStream os = new FileOutputStream(new File(path,fileName)) ;
     InputStream is = item.getInputStream() ;
     byte[] buffer = new byte[400] ;
     int length = 0 ;
     while((length = is.read(buffer)) != -1)
     {
      os.write(buffer,0,length) ;
     }
     is.close() ;
     os.close() ;
      
     
     
    }
   
  }
  
  
 }catch(Exception ex)
 {
  ex.printStackTrace() ;
 }
 request.getRequestDispatcher("fileUploadResult.jsp").forward(request, response) ;

 }

}

阅读(2273) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~