jsp上传一个文件夹下的所有文件

求高手。我想让客服端选择一个文件夹(不是单个文件),然后可以将这个文件夹下所有的.jpg文件上传到服务器,请问能不能实现,有高手有参考代码?邮箱1146817451.qq.com,如果可以运行,采纳后加100分。谢谢高手来解惑。

jsp上传一个文件夹下的所有文件:

1、上传的upload.jsp:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<form method="post" action="UploadServlet" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>


2、后台servlet:

public class FileUploadExample extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException {
           boolean isMultipart = ServletFileUpload.isMultipartContent(request);

           if (isMultipart) {
               // Create a factory for disk-based file items
               FileItemFactory factory = new DiskFileItemFactory();

               // Create a new file upload handler
               ServletFileUpload upload = new ServletFileUpload(factory);

               try {
                   // Parse the request
                   List items = upload.parseRequest(request);
                   Iterator iterator = items.iterator();
                   while (iterator.hasNext()) {
                       FileItem item = (FileItem) iterator.next();
                       if (!item.isFormField()) {
                           String fileName = item.getName();    
                           String root = getServletContext().getRealPath("/");
                           File path = new File(root + "/uploads");
                           if (!path.exists()) {
                               boolean status = path.mkdirs();
                           }

                           File uploadedFile = new File(path + "/" + fileName);
                           System.out.println(uploadedFile.getAbsolutePath());
                           item.write(uploadedFile);
                       }
                   }
               } catch (FileUploadException e) {
                   e.printStackTrace();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-07-09
可以的,jsp加什么?servlet或者是struts?
我可以写个demo给你,不知道你说的客户端是什么意思?
可以在jsp页面选择一个文件夹的路径,然后点上传,然后该文件夹下的所有.jpg文件都上传到指定的服务器目录下,这样能不能满足你的需求?追问

想要个servlet的,有demo满足。

追答

已经发送到你邮箱了,可以到我百度空间看看相关的使用方法
http://hi.baidu.com/huangyuewang99/item/2b56e1e63466a73f4cdcaf59

本回答被提问者采纳
相似回答