将文件上传到eclipse中的文件夹[复制]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我正在尝试通过我的Web应用程序上传文件并阅读上传的文件。首先,我可以将其上传到我的桌面。但这不是我想要的。我想在Eclipse中的项目资源管理器中创建一个名为file的目录(你可以在图中看到)。然后将文件上传到那里。我尝试了很多方法来提供路径,但它总是给我这个例外:由于java.io.FileNotFoundException而导致文件上传失败。这是我的项目explorer.enter image description here

https://ibb.co/niTaN6这里是图像

这是我的代码。

package com.fileupload;
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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.annotation.WebServlet;
@WebServlet("/UploadFile")
public class UploadFile extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String UPLOAD_DIRECTORY = "/GraphCoverage/file/";

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

// process only if its multipart content
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<FileItem> multiparts = upload.parseRequest(request);

           for (FileItem item : multiparts) {
           if (!item.isFormField()) {
           String name = new File(item.getName()).getName();
           item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
           }
        }

        // File uploaded successfully
        request.setAttribute("message", "Your file has been uploaded!");
        } 
        catch (Exception e) 
        {
         request.setAttribute("message", "File Upload Failed due to " + e);
        }
} else 
{
request.setAttribute("message", "This Servlet only handles file upload request");
}
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
java eclipse servlets file-upload
1个回答
0
投票

您好chalesea23您需要提供完整的系统路径,如“C:/ Users / abc / eclipse-workspace / Test / file”,然后它将工作。因为eclipse中的文件夹只是PC上的文件夹。

以下是适合我的。

package com.java;

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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.annotation.WebServlet;



@WebServlet("/UploadFile")
public class UploadFile extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final String UPLOAD_DIRECTORY = "C:/Users/abc/eclipse-workspace/Test/file";

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

        // process only if its multipart content
        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<FileItem> multiparts = upload.parseRequest(request);

                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {
                        String name = new File(item.getName()).getName();
                        item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                    }
                }

                // File uploaded successfully
                request.setAttribute("message", "Your file has been uploaded!");
            } catch (Exception e) {
                request.setAttribute("message", "File Upload Failed due to " + e);
            }
        } else {
            request.setAttribute("message", "This Servlet only handles file upload request");
        }
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.