Java Servlet应用程序文件上传失败[重复]

问题描述 投票:0回答:1
我正在尝试使用java servlet创建到File Upload并存储到本地目录。我添加了cos.jr文件。我向roor direcorty添加了一个新文件夹,名称为new。但是,当我单击按钮文件时,上传它会给我以下错误。

java.lang.IllegalArgumentException:不是目录:d:/ new

这是html。

<html> <body> <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> Select File:<input type="file" name="fname"/><br/> <input type="submit" value="upload"/> </form> </body> </html>

这是servlet代码。

package Servlet.org; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.MultipartRequest; public class FileUploadServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); response.setContentType("text/html"); PrintWriter out = response.getWriter(); MultipartRequest m=new MultipartRequest(request,"d:/JAVAServlet/new"); out.print("successfully uploaded"); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }

这里是我单击上传按钮时的结果。

enter image description here

java servlets
1个回答
1
投票
我认为这可能是路径或某些权限问题。不确定该原因为何显示为用d:/new代替d:/JAVAServlet/new。尝试下面的代码,看看是否创建目录。

public static void main(String[] args) { String saveDirectory = "d:" + File.separator + "JAVAServlet" + File.separator+ "new"; File dir = new File(saveDirectory); if(!dir.isDirectory()){ throw new IllegalArgumentException("Not a directory: " + saveDirectory); } }

目录创建权限或路径可能有问题。或尝试使用File.separator代替/"d:" + File.separator + "JAVAServlet" + File.separator+ "new"
© www.soinside.com 2019 - 2024. All rights reserved.