使用表单元素上载图像时出错

时间:2018-05-15 08:33:44

标签: java forms file jsp exception

问题陈述:

我正在尝试上传图片以供用户注册。

当我在表格中上传没有任何表格元素的图片时。它会正常运行。

但是当我尝试使用表单内的元素上传图像时     将提供更多信息的错误,请参阅提供的代码和错误信息。

注册用户的表格:

            <form method="post" action="UploadServlet"
              enctype="multipart/form-data">
                 username:<input type="text" name="username">
                 password:<input type="password" name="password">
                 city:<input type="text" name="city">
                 State:<input type="text" name="state">
                 Email:<input type="text" name="email">
                 Select file to upload: <input type="file" name="file" size="60" />
                 <br />
                 <br /> 
                 <input type="submit" value="Upload" />
           </form>

的Servlet

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
    maxFileSize = 1024 * 1024 * 10, // 10MB
    maxRequestSize = 1024 * 1024 * 50)   // 50MB
public class UploadServlet extends HttpServlet {

private static final String SAVE_DIR = "uploadFiles";

@Override
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String appPath = request.getServletContext().getRealPath("");
    String savePath = appPath + File.separator + SAVE_DIR;
    File fileSaveDir = new File(savePath);
    if (!fileSaveDir.exists()) {
        fileSaveDir.mkdir();
    }

    for (Part part : request.getParts()) {
        String fileName = extractFileName(part);
        fileName = new File(fileName).getName();
        part.write(savePath + File.separator + fileName);
    }
    request.setAttribute("message", "Upload has been done successfully!");
    getServletContext().getRequestDispatcher("/userlist.jsp").forward(
            request, response);
}

private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length() - 1);
        }
    }
    return "";
    }}

错误:

java.io.IOException: java.io.FileNotFoundException: E:\New 
Folder\UploadServlet30\build\web\uploadFiles (Access is denied)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.io.FileNotFoundException: E:\New 
Folder\UploadServlet30\build\web\uploadFiles (Access is denied)
java.io.FileOutputStream.open0(Native Method)
java.io.FileOutputStream.open(FileOutputStream.java:270)
java.io.FileOutputStream.<init>(FileOutputStream.java:213)
java.io.FileOutputStream.<init>(FileOutputStream.java:162)
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem. java:378)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

1 个答案:

答案 0 :(得分:1)

在你的servlet中,

doPost方法首先使用request.getParameter(&#34; inputname&#34;)检索所有参数。

之后

而不是使用foreach循环。 只写

    Part part = request.getPart("file");
    String fileName = extractFileName(part);
    part.write(savePath + File.separator + fileName);