Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

upload file with JSP and FileUpload.1.1

  Asked By: Bastet    Date: Feb 09    Category: Java    Views: 2646
  

I am writing a program for uploading a file onto my webserver with JSP. I use JSP, Tomcat, Apache2, MySQL on WindowsXP as my development environment.

I use this program : http://jakarta.apache.org/commons/fileupload/ . I choose the version 1.1.

But it does not run properly as I expect (HTTP Status 500).

// This is my 'ps.jsp' :
<body>
<table cellpadding="0" cellspacing="0" border="1" align="center">
<form enctype="multipart/form-data" action="http://localhost:8080/penguin-teknologi/penguin-teaches/cgiupload.jsp" method="post" name="myform">
<tr><td align="justify"><font style="Comic Sans MS, Arial">Silahkan pilih file yg akan diupload.</font></td></tr>
<tr><td><input type="text" name="nama"></td></tr>
<tr><td align="justify"><input type="file" name="soalnya"></td></tr>
<tr><td align="justify"><input type="submit" value="Kirim"><input type="reset"></td></tr>
</form>
</table>
</body>
-----
// This is my 'cgiupload.jsp'
<%@ page import="org.apache.commons.fileupload.*, java.util.List, java.io.File, java.util.Iterator" %>
<%
boolean isMultipart = FileUpload.isMultipartContent(request);
if(!isMultipart){
request.setAttribute("msg", "Request was not multipart!");
request.getRequestDispatcher("msg.jsp").forward(request, response);
return;
}

DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(request);
Iterator itr = items.iterator();

while(itr.hasNext()){
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
String fieldName = item.getFieldName();
if(fieldName.equals("name"))
request.setAttribute("msg", "Thank You: " + item.getString());
} else {
File fullFile = new File(item.getName());
File savedFile = new File(getServletContext().getRealPath("/"), fullFile.getName());
item.write(savedFile);
}
}

request.getRequestDispatcher("msg.jsp").forward(request, response);
%>
-----
// This is my 'msg.jsp' :
<body>
<%
String msg = (String)request.getAttribute("msg");
if(msg != null)
out.println("<font size=+1>Buseettt<br>" + msg + "</font><br/>");
%>
Click <a href="../../../../../Apache%20Group/Apache2/htdocs/p-tek/penguin-teaches/ps.jsp">here</a> to go to the upload page.
</body>
-----
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause

javax.servlet.ServletException: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.penguin_002dteaches.cgiupload_jsp._jspService(cgiupload_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.DefaultFileItemFactory.createItem(DefaultFileItemFactory.java:102)
org.apache.commons.fileupload.FileUploadBase.createItem(FileUploadBase.java:500)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:268)
org.apache.jsp.penguin_002dteaches.cgiupload_jsp._jspService(cgiupload_jsp.java:57)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Terence Mitchell     Answered On: Feb 09

You should have commons-io in your class path

 
Didn't find what you were looking for? Find more on upload file with JSP and FileUpload.1.1 Or get search suggestion and latest updates.




Tagged: