0

下面是我用来将文件上传到服务器的代码。但是代码抛出异常目录或找不到文件..

                ResourceBundle rs_mail = ResourceBundle.getBundle("mail");
                String upload_path = rs_mail.getString("upload_path");
                File file = null;
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                // Parse the request to get file items.
                List fileItems = upload.parseRequest(request);

                // Process the uploaded file items
                Iterator i = fileItems.iterator();
                while (i.hasNext()) {
                    FileItem fi = (FileItem) i.next();
                    File uploadDir = new File(upload_path);
                    if (!uploadDir.exists()) {
                        uploadDir.mkdir();
                    }
                  file = new File(upload_path + file.separator + fi.getName());

                    fi.write(file);
                }

谁能指出异常的原因..

属性文件的内容

上传路径=../../../上传

4

1 回答 1

1

确保您还在以下路径上创建了所有父目录upload_path

if (!uploadDir.exists()) {
   uploadDir.mkdirs();
}

注意使用mkdirs()代替mkdir()mkdir()如果父结构不存在,将失败。mkdirs()还将尝试创建所需的父目录。

您还应该检查返回值,false如果无法创建目录,这两种方法都将返回。

于 2013-10-09T09:21:24.153 回答