File Uploading to Specific Directory
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Girish_Manghani
Posted On:   Friday, April 6, 2001 10:22 AM

I want a servlet which uploads the Files to a specific directory in the server root. I will give a select box to the user who will choose the directory and pass it on to the servlet as a parameter and the servlet should upload that file to the that directory. I tried to modify and use O'reilly's MultipartRequest, but in vain, please suggest a solution. It is very urgent.

Re: File Uploading to Specific Directory

Posted By:   vaibhav_jain  
Posted On:   Sunday, April 8, 2001 07:00 AM

Hi girish,


I have used oreillys multipartrequest and it working fine. Please refer to the code I have used. I am givig the code only for the servlet that uploads the file and not the html form which does call the servlet...


See below...


import java.net.*;
import java.sql.*;
import java.awt.*;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import com.oreilly.servlet.MultipartRequest;
import com.javaexchange.dbConnectionBroker.*;


public class ImageUpLoad extends HttpServletJXGB{
Connection cx = null;
Statement st;
ResultSet rs;
Image img;
int id;
String filename;
MultipartRequest multi;
public void init(ServletConfig config) throws ServletException
{
try
{
super.init(config);
}
catch(Exception e)
{
System.out.println(e);
}
}


public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
res.setContentType("text/html");
PrintWriter out =res.getWriter();
try{
try{
cx = myBroker.getConnection();
st=cx.createStatement();
}
catch(Exception e){
System.err.println(e);
}

String id="";

try{
multi =new MultipartRequest(req,"absolute path to server directory",5*1024*1024);
}
catch(Exception e)
{
out.println("xception multi:"+e);
}

Enumeration enum=multi.getParameterNames();
while(enum.hasMoreElements()){
String name=(String)enum.nextElement();
String value=multi.getParameter(name);
//out.println("name="+name);
//out.println("value="+value);
id=value;
}
//o//ut.println("

Files

");
Enumeration files=multi.getFileNames();
while(files.hasMoreElements())
{
String name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
File f= multi.getFile(name);
//out.println("name="+name);
//out.println("filename="+filename);
//out.println("type="+type);
if(f !=null)
{
//out.println("length:="+f.length());
//out.println();
}
}
System.out.println(id);
String query="";
//String str="c:\testimages"+filename;


query="update male_mating set image_path='"+filename+"' where id='"+id+"'";


st.executeUpdate(query);
//System.out.println(str);


}

catch(Exception e)
{
System.out.println("e:"+e);
}
finally {
try{if(st != null) {st.close();}} catch(SQLException e1){};

// The connection is returned to the Broker
myBroker.freeConnection(cx);
}

// out.close();

}
}



Hope this solves your problem....
About | Sitemap | Contact