• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Upload using commons-fileupload-1.0

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have written a jsp which uploads a file on to the server.
The problem iam facing is that in the servlet which iam calling through the jsp (the which uploads file) I am not getting any data in the request. I tried this using commons-fileupload-1.0 package and com.oreilly.servlet package. I triedreading the file name of the uploaded file. I got proper data for that that. But it shows that the size of the uploaded file is 0 bytes.
I am using Tomcat as my webserver.
Is there some specific setting to be done in the Web Server for the file upload.
I am including my Jsp and servlet code below.
-------------------------------JSP CODE-----------------------------------
<html>
<head>
<title>Upload File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%@ page import="com.trimemfp.common.model.*,com.trimemfp.general.model.*,com.trimemfp.quiz.controller.*"%>
<script language=javascript>
function fnDisplayMsg( formElement )
{
var error ;
error = formElement.ERROR_CODE;
alert(error);
return false ;
}
function fnSubmit( )
{
var formObject = document.forms[0] ;
var filename = document.forms[0].uploadfile.value ;
if( filename == "" )
{
formObject.uploadfile.ERROR_CODE = "ERROR: FILE LOCATION REQUIRED" ;
fnDisplayMsg( formObject.uploadfile ) ;
return false ;
}
var name = new String( filename ) ;
var indexno= filename.lastIndexOf(".") ;
if( indexno == -1 )
{
formObject.uploadfile.ERROR_CODE = "ERROR: FILE NOT FOUND" ;
fnDisplayMsg( formObject.uploadfile ) ;
return false ;
}
filename = name.substring(indexno, indexno + 4) ;
alert(filename);
if(filename != ".xls")
{
formObject.uploadfile.ERROR_CODE = "ERROR: INCORRECT FILE EXTENSION" ;
fnDisplayMsg( formObject.uploadfile ) ;
return false ;
}
formObject.submit( ) ;
return true;
}
</script>
<body bgcolor="#F1F2CE">
<p></p> 
<p></p> 
<p></p> 
<p></p> 
<FORM ENCTYPE='multipart/form-data' method='POST' action='/Member/QuizController?page=upload'>
<INPUT TYPE='file' NAME='uploadfile' size="40" maxlength="255">
<INPUT TYPE='submit' VALUE='Upload File' >
</FORM>
</body>
</head>
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-------------------------------SERVLET CODE--------------------------------
package com.trimemfp.quiz.controller;
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import com.oreilly.servlet.multipart.*;
import com.trimemfp.general.model.*;
import com.trimemfp.general.bob.*;
import com.trimemfp.common.model.*;
public class QuizController extends HttpServlet
{
public void doPost (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
String action = request.getParameter("page");
System.out.println(1);
if(action.equals("upload"))
performUploadAction(request,response);
}

private void performUploadAction(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
String username;
String password;
LoginBob lbob;
MessageBean message;
LoginModel login;
HttpSession session = request.getSession(true);

MultipartParser mp = new MultipartParser(request, 10*1024*1024); Part part;
while ((part = mp.readNextPart()) != null)
{
String name = part.getName();
if (part.isFile())
{
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null)
{
System.out.println("file: name=" + name + "; fileName=" + fileName + ", filePath=" + filePart.getFilePath() + ", contentType=" + filePart.getContentType());
insertData(filePart.getInputStream());
try{
FileOutputStream fo = new
FileOutputStream"C:\tp1.xls");
filePart.writeTo(fo);
}
catch(Exception e)
{
System.out.println(e);
}
}
else
{
System.out.println("file: name=" + name + "; EMPTY");
}
}
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic