简体   繁体   English

在jsp中上传文件

[英]Upload file in jsp

i am trying to upload file using this code : 我正在尝试使用以下代码上传文件:

    package controle;
    @WebServlet("/upload")
    @MultipartConfig
    public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String id;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    uploadFile(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    id = (String) request.getAttribute("id");
    uploadFile(request, response);
}

private void uploadFile(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    for (Part part : request.getParts()) {
        String name = part.getName();
        InputStream is = request.getPart(name).getInputStream();
        String fileName = getUploadedFileName(part);
        FileOutputStream fos = new FileOutputStream(
                "//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"
                        + fileName);
        int data = 0;
        while ((data = is.read()) != -1) {
            fos.write(data);
        }
        fos.close();
        is.close();
        response.sendRedirect("success.jsp");
    }
}

private String getFormat(String fileName) {
    String format = "none";
    int index = fileName.lastIndexOf(".");
    if (index > 0) {
        format = fileName.substring(index + 1);
        format = format.toLowerCase();
    }
    return format;
}

private String getUploadedFileName(Part p) {
    String file = "", header = "Content-Disposition";
    String[] strArray = p.getHeader(header).split(";");
    for (String split : strArray) {
        if (split.trim().startsWith("filename")) {
            file = split.substring(split.indexOf('=') + 1);
            file = file.trim().replace("\"", "");
            System.out.println("File name : " + file);
        }
    }
    return file;
}

} }

but when irun my application i get this error message: 但是当我运行我的应用程序时,我收到此错误消息:

java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileapp (Accès refusé)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(Unknown Source)
    java.io.FileOutputStream.<init>(Unknown Source)
    controle.FileUploadServlet.uploadFile(FileUploadServlet.java:45)
    controle.FileUploadServlet.doPost(FileUploadServlet.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

what went wrong???? 什么地方出了错????

Make sure there is a folder by the name imagesapp under the WEB-INF and as you have 确保在WEB-INF下有一个名为imagesapp的文件夹

//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"

which means it will append the fileName to profileapp and store it in imagesapp .But if you are assuming that the file will be kept under profileapp then just put a slash atfer it like this 这意味着它将会追加fileNameprofileapp并将其存储在imagesapp 。但如果你是假设该文件将在profileapp保持那么只要把一个斜线atfer像这样

C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp/"

In uploadFile method: 在uploadFile方法中:

    String name = part.getName();
    InputStream is = request.getPart(name).getInputStream();

You already has the part to get the input stream and are using getPart(name) to get the same instance part above. 您已经具有获取输入流的部分,并且正在使用getPart(name)获取上面的相同实例部分。

I will not recommend to get the input stream to write after to a file with some output stream. 我不建议将输入流写入具有某些输出流的文件中。 Instead if it do the following: 相反,如果执行以下操作:

    String fileName = part.getHeader("content-disposition").replaceAll(".+filename=\"(.+)\"", "$1");
    String fileFormat = fileName.replaceAll(".+[.](.+)$", "$1");
    part.write("//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp/" + filename);

note here: 注意这里:

    "//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"
           + fileName

is what you did. 是你做的 The final / is missing in profileapp. profileapp中缺少最后一个/。

The message error throws: 消息错误抛出:

   java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileapp

profileapp is a folder. profileapp是一个文件夹。 The message shows that your method return an empty string defined as "" at the begin of the method. 该消息显示您的方法在该方法的开头返回一个空字符串,定义为“”。 If your method returns "SOME_STRING", you will see the following message error if the folder imagesapp doesn't exists: 如果您的方法返回“ SOME_STRING”,则如果文件夹imagesapp不存在,您将看到以下消息错误:

      java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileappSOME_STRING

Otherwise you will create the profileappSOME_STRING file inside imagesapp folder. 否则,您将在imagesapp文件夹内创建profileappSOME_STRING文件。

If your profileapp folder doesn't exists, you would create the file profileapp inside imagesapp folder without exception. 如果您的profileapp文件夹不存在,则将毫无例外地在imagesapp文件夹内创建文件profileapp。 Not that you had an Access refuse. 并不是说您有访问拒绝。 You are trying to write the content in a folder as a file. 您正在尝试将内容作为文件写入文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM