在servlet中上传文件时遇到问题

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

我正在关注Servlet中的文件上传教程

我的系统配置是

Eclipse

Apache Tomcat 8.5

Jdk:1.8

我的index.html(表单代码)是

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="UploadFileServlet" method="post">
    Select a file to upload 
    <input type="file" value="file" name="source"/> 
    <input type="submit" value="Upload"/>
</form>
</body>
</html>

UploadFileServlet.java

package com.upload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


    /**
     * Servlet implementation class UploadFileServlet
     */
    @WebServlet("/UploadFileServlet")
    public class UploadFileServlet extends HttpServlet 
    {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {
            try
            {
            ServletFileUpload sf=new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> multFileItems=sf.parseRequest(request);
            System.out.println(multFileItems.isEmpty());
            for(FileItem item:multFileItems)
            {
                item.write(new File("E:\\aman\\"+item.getName()));
            }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }//End of doPost method
    }

我不明白为什么它不起作用。

它不会产生任何异常。

预期输出

我希望此代码将我在表单中选择的文件上传到e:\ aman \文件夹中,但不会上传任何文件

您可以通过以下方式重现此错误:>>

https://drive.google.com/open?id=1oHnkV5Tmrp2jX8SU6Ilw_EYPWXQbsvv9

我正在Servlet中查看文件上传教程,我的系统配置是Eclipse Apache Tomcat 8.5 Jdk:1.8我的index.html(表单代码)是

<...>
java jsp servlets jakarta-ee file-upload
1个回答
0
投票

我更改了index.html

© www.soinside.com 2019 - 2024. All rights reserved.