2
Contributors
7
Replies
24
Views
3 Years
Discussion Span
Last Post by msolomon.ashish
0

i want to upload image to tomcat server by creating a folder with username and upload image to username and imagepath to database to retreive image easily.i want code in jsp or servelet.

Edited by msolomon.ashish: nothing

0

so .. you want to upload it both to your server and to a db? what's the point of that?

what have you got so far?

0

i am getting folder with username but its not uploading image to username folder.please give the code i am working with social networking site so i want this code.

0

i want to upload image to tomcat server by creating a folder with username and upload image to username and only imagepath to database to retreive image easily.i want code in jsp or servelet.

0
<%@page import="javax.swing.text.Utilities"%>
<%@page import="org.apache.commons.io.FilenameUtils"%>
<%@page import="java.sql.*"%>

<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*, java.io.*" %>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.io.File"%>
<%@ page contentType="text/html;charset=UTF-8" %>

<%

/*String ImageFile="";
String itemName = "";
String user = session.getAttribute("username").toString();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
{
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try
{
items = upload.parseRequest(request);
}
catch (FileUploadException e)
{
e.getMessage();
}

Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
if(name.equals("ImageFile"))
{
ImageFile=value;
}

}
*/
//else
//{
      File savedFile;
    String destination;

    List<FileItem> items = null;
    try {
        items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (FileItem item : items) {
        if (item.isFormField()) {
            // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
        } else {

           String fieldName = item.getFieldName();
            String fileName = FilenameUtils.getName(item.getName());
            InputStream fileContent = item.getInputStream();
            String userName = session.getAttribute("username").toString();
 File savedFile1 = new File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+userName+".jpeg" );
          //  String fileName1="/" +"uploadedFiles/" + userName + ".jpeg";
 //destination = getServletContext().getRealPath(fileName);
// savedFile = new File(destination);

    if(! savedFile1.exists()) 
         savedFile1.mkdir();
    // savedFile1=new File( savedFile1 + "/" ); 
     // savedFile1.createNewFile();
     //  item.write(savedFile1);

File uploadedFile = new File("savedFile" );
                        System.out.println(uploadedFile.getAbsolutePath());
                        item.write(savedFile1);
     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(savedFile1));
      byte[] buffer = new byte[1024];
            int len;

            //Read from file and write to new file destination
            while((len = fileContent.read(buffer)) >= 0) {
                bos.write(buffer, 0, len);
            }
             fileContent.close();
            bos.close();

}
    }

try
{

    Class.forName("com.mysql.jdbc.Driver");

Connection  con=DriverManager.getConnection("jdbc:mysql://localhost:3306/classified","root","12345");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into image (image) values (?)");
 response.sendRedirect("welcomeuser.jsp");
}
catch(Exception el)
{
out.println("Inserting error"+el.getMessage());
}
}
}
catch (Exception e){
out.println(e.getMessage());
}
%>

Edited by msolomon.ashish: no

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.