码迷,mamicode.com
首页 > Web开发 > 详细

使用Apache fileUpload库上传文件(支持多个文件)

时间:2015-05-14 20:36:10      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:java

Spring 3.0验证通过

 

import org.apache.commons.fileupload.DiskFileUpload;

import org.apache.commons.fileupload.FileItem;

 

  public String upload(HttpServletRequest request,

      HttpServletResponse response) throws Exception {

    String fullFileName = null;

    DiskFileUpload disk = new DiskFileUpload();

    try {

      List<FileItem> files = (List<FileItem>) disk.parseRequest(request);

      for (FileItem item : files) {

        fullFileName = item.getName();

        if(fullFileName == null) {

          System.out.println("fullFileName not existed.");

          continue;

        }

        String fileName1 = fullFileName.substring(fullFileName.lastIndexOf("\\")+1); 

        File  file1 = new File(realPath,fileName1);

                file1.getParentFile().mkdirs();

                file1.createNewFile();

               

                InputStream ins = item.getInputStream();

                OutputStream ous = new FileOutputStream(file1);

               

                try{

                    byte[] buffer = new byte[1024];

                    int len = 0;

                    while((len = ins.read(buffer)) > -1)

                        ous.write(buffer,0,len);

                   

                }finally{

                    ous.close();

                    ins.close();

                }

      }

    } catch (Exception e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

 

    return "redirect:item.do";

  }

<form action="item.do " enctype="multipart/form-data" method="post">

<input name="itemFile" type="file" class="text1" size="40" maxlength="40">(必须是.gif或.jpg文件)

</form>

 


使用Apache fileUpload库上传文件(支持多个文件)

标签:java

原文地址:http://blog.csdn.net/zxygww/article/details/45725195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!