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> }
这里是我单击上传按钮时的结果。
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"