Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file uploading through servlet

 
biswajit ray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody help me in file uploading in servlet?
 
biswajit ray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
* Created on Mar 14, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.thinkways.javafiles;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Date;
import java.util.Enumeration;
import com.thinkways.javafiles.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AudioRequestUpload extends HttpServlet
{

private String dirName;
//conBean ob;
// Conn con;
// Connection cn;
ContactOperation db;
MultipartRequest multi=null;

public AudioRequestUpload()
{
//con = new Conn();
// obo = new Project();
//ob=new conBean();
//private final String dirName="";
db=new ContactOperation();

}
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//MultipartRequest multi = null;
java.io.PrintWriter out = response.getWriter();
response.setContentType("text/plain");
//out.println("i m in before try block");
try
{
System.out.println("I M HERE123####");
int adminflag = 0;
//int Audioname_Id=0;
String audioid="";
int audio_id=0;
audioid=request.getParameter("audioid");
System.out.println("<<<<audioid is>>>>>"+audioid);
audio_id=Integer.parseInt(audioid);
System.out.println("audio_id is"+audio_id);
String titlename=request.getParameter("titlename");
System.out.println("<<<<titlename is>>>>>>>"+titlename);
//int flag1=db.searchtitlename(titlename);
HttpSession session=request.getSession();
String appid=(String)session.getAttribute("APPLICATIONID") ;
System.out.println("the applicationid in the audio request upload is"+appid);
//String titlename=(String)session.getAttribute("TITLENAME");
//System.out.println("titlename is>>>>>>>>>>>"+titlename);
//String titlename=request.getParameter("n1");
// System.out.println("The TitleName in the audio request upload is>>>>>>>>>"+titlename);
dirName = request.getRealPath("/")+"AudioDocument";
//File file=new File(dirName);
//boolean t=file.isDirectory();
//System.out.println("t is "+t);
//if(t==false)
// {
// boolean r=file.mkdir();
//System.out.println("r is"+r);

// }
System.out.println("the dirname is"+dirName);
System.out.println("now i m here");
// String audio_name_Id=request.getParameter("Audioname_Id");
//System.out.println("The Audioname_Id in the request upload is"+audio_name_Id);
//String audio_id=(String)session.getAttribute("AUDIOID");
//Audioname_Id=Integer.parseInt(audioid);
//System.out.println("The Audio_id is>>"+Audioname_Id);
//String categoryname=(String)session.getAttribute("CATEGORYNAME") ;
// System.out.println("the CATEGORYNAME is"+categoryname);

//String str=(String)request.getParameter("headline");
//System.out.println("str is"+str);
//int categoryid=Integer.parseInt((String)session.getAttribute("CATEGORYID"));
//System.out.println("the categoryid is"+categoryid);
//File file=new File("AUDIO\\"+appid);
// System.out.println(request.getContextPath()+"/AUDIO/"+appid.substring(0,5));
/* boolean t;

t=file.isDirectory();*/

//System.out.println("the t is"+t);
// String path="";
// if(t==false)
//{
//System.out.println("inside if");

//boolean r=file.mkdirs();
//System.out.println("r is"+r);
System.out.println(" i m before multi");
long lastmodification=new Date().getTime();
System.out.println("lastmodification is"+lastmodification);
String lastmodify1=""+lastmodification;
try
{
multi= new MultipartRequest(request, dirName, 0xa00000, "ISO-8859-1", new DefaultFileRenamePolicy());
}
catch (Exception e){
System.out.println ("exception in upload : " + e.getMessage());
}
System.out.println("i m after for multi");

//path=file.getAbsolutePath();
//System.out.println("path is"+path);
for(Enumeration params = multi.getParameterNames(); params.hasMoreElements();)
{

String name = (String)params.nextElement();
String s = multi.getParameter(name);
}

for(Enumeration files = multi.getFileNames(); files.hasMoreElements();)
{
String name = (String)files.nextElement();
String filename1 = multi.getFilesystemName(name);
String originalFilename = multi.getOriginalFileName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
System.out.println("inside the demorequestupload");
if(f != null)
{
// getServletContext().getRequestDispatcher("//jsp/viewpage.jsp").forward(request, response);
String filename = f.getName();
System.out.println("the filename is"+filename);
int j=filename.lastIndexOf(".");
System.out.println("j is "+j);
String format=filename.substring(j+1);
System.out.println("format is"+format);
String filepath = f.toString();
System.out.println("the filepath is"+filepath);
String version="1";
int flag=db.audiouploadsave(titlename,filepath,audio_id,appid,lastmodify1,version,format);
System.out.println("FLAG IS"+flag);
if(flag==1)
{
System.out.println("<<<<<<<<<,sucessfully saved>>>>>>>");
String message="SUCCESSFULLY UPLOADED";
session.setAttribute("MESSAGE",message);
response.sendRedirect("/MOBISYNC/jsp/Audioupload.jsp?mycheck="+1+" ");
//request.getRequestDispatcher("/jsp/a1.jsp").forward(request,response);
//getServletContext().getRequestDispatcher("/jsp/a1.jsp").forward(request, response);
//getServletContext().getRequestDispatcher("/MOBISYNC/jsp/Audioupload.jsp").forward(request, response);
////request.getRequestDispatcher("/MOBISYNC/jsp/Audioupload.jsp").forward(request,response);

}
else if(flag==2)
{
String message="THIS TITLENAME EXIST PLEASE CHOOSE ANOTHER ONE";
session.setAttribute("MESSAGEPRESENT",message);
response.sendRedirect("/MOBISYNC/jsp/Audioupload.jsp?mycheck2="+1+" ");
///getServletContext().getRequestDispatcher("/jsp/a2.jsp").forward(request, response);


}
// int flag = obo.storeFile(filename, filepath);
//int storeflag=1;
//int flag=ob.storeFile(subcategoryid,headline,filepath,filename,storeflag);
//int flag=ob.storeaudiorecord(categoryname,path,lastmodify1,appid);
// if(flag == 1)
//{
//System.out.println("file and path is saved<<<<<<<<<");

}
// else
//{
// System.out.println("not saved<<<<<<<<");
// }

}

// }

// }

// else if(t==true)
// {
// System.out.println("inside 2nd if");
//path=file.getAbsolutePath();
//MultipartRequest multi1 = new MultipartRequest(request, dirName, 0xa00000, "ISO-8859-1", new DefaultFileRenamePolicy());
// for(Enumeration params = multi1.getParameterNames(); params.hasMoreElements();)
// {

//String name = (String)params.nextElement();
// String s = multi1.getParameter(name);
// }

// for(Enumeration files = multi1.getFileNames(); files.hasMoreElements();)
// {
//String name = (String)files.nextElement();
// String filename1 = multi1.getFilesystemName(name);
// String originalFilename = multi1.getOriginalFileName(name);
// String type = multi1.getContentType(name);
// File f = multi1.getFile(name);
// System.out.println("inside the demorequestupload");
// if(f != null)
// {
// getServletContext().getRequestDispatcher("//jsp/viewpage.jsp").forward(request, response);
//String filename = f.getName();
//System.out.println("the filename is"+filename);
//String filepath = f.toString();
//System.out.println("the filepath is"+filepath);
//long lastmodification1=new Date().getTime();
//System.out.println("lastmodification is"+lastmodification1);
//String lastmodify=""+lastmodification1;
// int flag = obo.storeFile(filename, filepath);
//int storeflag=1;
//int flag=ob.storeFile(subcategoryid,headline,filepath,filename,storeflag);
//int flag=ob.storeaudiorecord(categoryname,path,lastmodify,appid);
// if(flag == 1)
//{
// System.out.println("file and path is saved<<<<<<<<<");
// getServletContext().getRequestDispatcher("/jsp/successpage.jsp").forward(request, response);
// }

// else
// {
// System.out.println("not saved");
//}
// }
//else
// {
// System.out.println("####not saved");
// }

// }

//}



}
catch(Exception lEx)
{
getServletContext().log(lEx, "error reading or saving file");
}
// }
}
}





/*public String makeDir()
{
File file = new File(System.getProperty("user.dir") + "\\brimingham\\");
file.mkdir();
return file.getAbsolutePath();
}
*/
// Conn con;
// Connection cn;
// Project obo;
// conferencebean ob;
// private String dirName;
//}
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aslo check http://faq.javaranch.com/view?FileUpload
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64978
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a JSP issue. Moved to Servlets.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is working, what isn't? What are you expecting it to do and what isn't it doing? TellTheDetails

My first suggestion is to remove all the instance variables and instead use local variables in your doPost method to avoid threading issues.

Instance variables:
private String dirName;
//conBean ob;
// Conn con;
// Connection cn;
ContactOperation db;
MultipartRequest multi=null;

Then show us what kind of output you are getting and other details I mentioned above.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic