我正在使用eclipse galelio和apache tomcat server 5.5
jsp页面代码:
<form action="Add" method=post enctype="multipart/form-data">
<p>Book name: <input type="text" name="bname" required/></p>
<p>Price:<input type="text" name="bprice" required/></p>
<p>Quantity:<input type="text" name="bqty" required/></p>
<p>Image: <input type="file" name="file" required/></p>
<p>Course: <select name="course">
<option>course 1</option>
<option>course 2</option> <!-- Some more options-->
<input type="submit" value="Add" name="Submit"/></p>
</form>
servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String path="images/default";
MultipartRequest m=new MultipartRequest(request,path);
String cy="";// i am assigng the value to cy based on some if else conditions on the value of course field
String bname=m.getParameter("bname");
String bprice=m.getParameter("bprice");
String bqty=m.getParameter("bqty");
String course=m.getParameter("course");
String file=m.getFilesystemName("file");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","root");
PreparedStatement ps=con.prepareStatement("select * from product where bname=? and course=?");
ps.setString(1,bname);
ps.setString(2,course);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
out.print("<p style='position:absolute;top:130px;left:360px;color:#CC0066'>Product already exists! You might want to <b>update</b>.</p>");
}
else
{
ps=con.prepareStatement("insert into product(bname,cy,course,price,qty,path,type) values (?,?,?,?,?,?,'n')");
ps.setString(1,bname);
ps.setString(2,cy);
ps.setString(3,course);
ps.setString(4,bprice);
ps.setString(5,bqty);
ps.setString(6,path+"/"+file);
ps.executeUpdate();
out.print("<p style='position:absolute;top:160px;left:550px;color:#CC0066'>Product added!</p>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
RequestDispatcher rd=request.getRequestDispatcher("admin.jsp");
rd.include(request, response);
}
我收到此错误:
java.lang.IllegalArgumentException: Not a directory: images/default
我肯定在eclipse中的项目的webcontent文件夹中有目录。 它应该工作正常,但不是。问题出在哪里? 我导入了:import com.oreilly.servlet.MultipartRequest;并在我的lib中有cos.jar文件 看附图。
答案 0 :(得分:0)
正如您在类中的src代码中所看到的,路径必须存在并且必须是目录。所以我想目的地images/default
不存在。
/**
* Constructs a new MultipartRequest to handle the specified request,
* saving any uploaded files to the given directory, and limiting the
* upload size to the specified length. If the content is too large, an
* IOException is thrown. This constructor actually parses the
* <tt>multipart/form-data</tt> and throws an IOException if there's any
* problem reading or parsing the request.
* <p/>
* To avoid file collisions, this constructor takes an implementation of the
* FileRenamePolicy interface to allow a pluggable rename policy.
*
* @param request the servlet request.
* @param saveDirectory the directory in which to save any uploaded files.
* @param maxPostSize the maximum size of the POST content.
* @param encoding the encoding of the response, such as ISO-8859-1
* @param policy a pluggable file rename policy
* @throws IOException if the uploaded content is larger than
* <tt>maxPostSize</tt> or there's a problem reading or parsing the request.
*/
public MultipartRequest(HttpServletRequest request,
String saveDirectory,
int maxPostSize,
String encoding,
FileRenamePolicy policy) throws IOException {
// Sanity check values
if (request == null)
throw new IllegalArgumentException("request cannot be null");
if (saveDirectory == null)
throw new IllegalArgumentException("saveDirectory cannot be null");
if (maxPostSize <= 0) {
throw new IllegalArgumentException("maxPostSize must be positive");
}
// Save the dir
File dir = new File(saveDirectory);
// Check saveDirectory is truly a directory
if (!dir.isDirectory())
throw new IllegalArgumentException("Not a directory: " + saveDirectory);