Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet Full path of image to save it in a folder
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Sourabh Chourasia
Full path of image to save it in a folder
1 Answer(s)      2 years and 6 months ago
Posted in : JSP-Servlet


Sir ,I am trying to upload a file but only sends file name as parameter. I want a code that would help me to find that image path &upload it as well. I am just a beginner in jsp &trying to create a project.


Advertisement
View Answers

April 3, 2012 at 10:18 PM


Hi,

If you wish to upload files in the local context of your project, then you may first create a folder under WEB_INF. call it anything you want. Then considering the fact that you are a beginner, I would suggest that you create a servlet and call it from the page using method POST. Within the servlet, there would be two options for you to explore:- 1. saving the file uploaded into the database, which would require the use of either JDBC or JPA, whichever is known to you. 2. saving the file in a folder.

code is given below:-

package pack;

import java.io.*;
import java.sql.DriverManager;
import java.util.Scanner;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.annotation.MultipartConfig;

import java.sql.*;

/**
 * Servlet implementation class uploadServlet
 */
@WebServlet("/upload.do")
@MultipartConfig
public class uploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Part p1=request.getPart("title");
        Part p2=request.getPart("filesel");
        InputStream is=p2.getInputStream();
        Scanner sc=new Scanner(p1.getInputStream());
        String title=sc.next();
        sc=new Scanner(p2.getContentType());
        String type=sc.next();
        try
        {
            String path=request.getServletContext().getRealPath("folder url where u want to store your uploaded file");
            FileOutputStream fos=new FileOutputStream(path);
            byte buf[]=new byte[is.available()];
            is.read(buf);
            fos.write(buf);
            fos.flush();
            fos.close();
            // use this part only if you wish to store files in database
            Class.forName("sun.jdbc.odbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:odbc:databasename");
            PreparedStatement ps=con.prepareStatement("insert in table (picName,picPath) values(?,?) ");
            ps.setString(1, title);
            ps.setString(2, path);
            ps.execute();
            ps.close();
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        }
    }

Note that the part where I set the path for storage has to be either one of local context as mentioned before or it can be a folder created anywhere in your machine.

Hope this solves your query.

Regards, Abhineet Verma



Related Tutorials/Questions & Answers:
Full path of image to save it in a folder
Full path of image to save it in a folder  Sir ,I am trying to upload... to find that image path &upload it as well. I am just a beginner in jsp... in the local context of your project, then you may first create a folder under WEB
image save to folder in java
image save to folder in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open... folder and that path stored into database, for furhter retrivation. Please guide me
Advertisements
Java- Save Image into project folder in web application
Java- Save Image into project folder in web application  I have a requirement of saving the image into project folder .. which is a web application... want to save the image..when I am trying to create empty folder and get
write file in aplication folder with out giving full path - WebSevices
write file in aplication folder with out giving full path  i have a simple java class by the help of this java class i want to write file in application folder with out giving full path like c:/data/aa
Image name,image path into database and image into folder using jsp
Image name,image path into database and image into folder using jsp  How to insert image path and image name into oracle database and image into folder using jsp
i want to save image on server folder. and i want to save URL of this folder and name of image in database
i want to save image on server folder. and i want to save URL of this folder and name of image in database  hi i want to save image on server folder. and i want to save URL of this folder and name of image in database.on click
How to store user name,city,state,image path into database and image into folder using jsp
How to store user name,city,state,image path into database and image into folder using jsp  How to store user name,city,state,image path into database and image into folder using jsp
how to store image in folder and stored image path in mysql database using JSP
how to store image in folder and stored image path in mysql database using JSP  how to store image in folder and stored image path in mysql database using JSP
session_save_path()
session_save_path() session_save_path() command returns the current working directory path.  Syntax session_save_path(); Example: <?php echo session_save_path(); #return the current working directory path ?>  
How to get the full path location using <input type=file>
How to get the full path location using input type=file  Hi. I have used input type=file> in HTML to select an image. But it selects only the image but not with its full path location. how to do it? Thanks in advance
how to upload multiple files in jsp and saving the path in database and the file in folder
, for example, i need to save a image for the photo of candidate and resume...how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path
How to upload and save a file in a folder - Java Beginners
How to upload and save a file in a folder  Hi How to upload and save a file in a folder? for example Agreement.jsp File: So when... in solving the problem : Image Upload
inserting an path of an image in database - JDBC
time its full path should be inserted in the database(MS Sql 2000).. I m able to save it in folder..but can you plz tell me how an the full path of image can...inserting an path of an image in database  hello kindly help related
Jmagick write image to a specific folder
to save the resized image to a specific location try{ String path... and save the resized images to the another location. for example : get an image to resize from c:\myDirectory\images and save the resized image to c:\myDirectory
to obtain image path
"); // create a file object for image by specifying full path of image...to obtain image path   i have made a web application in which you can... or BROWSE button . and i am expecting to obtain the complete path of the image from
image upload in webapp/upload folder
image upload in webapp/upload folder  sir i want to store upload image in my project directory WebApp/Upload_image/ pls send the jsp servlet code when i upload the image one error found "system cannot found the specified path
save the uploading image with user specified name
save the uploading image with user specified name  My form consists of a textbox and an upload image with submit button.When i submit the form I want to save the uploaded image in a folder with user specfied name
save the uploading image with user specified name
save the uploading image with user specified name  My form consists of a textbox and an upload image with submit button.When i submit the form I want to save the uploaded image in a folder with user specfied name
How to save image on the server by applet
How to save image on the server by applet  HI, in my project I am... If user want to export this chart as GIF format then this chart should be save as a gif image on the server directory. Can any Body help me how can I do it Thanks
How to save image on the server by applet
How to save image on the server by applet  HI, in my project I am... If user want to export this chart as GIF format then this chart should be save as a gif image on the server directory. Can any Body help me how can I do it Thanks
how to insert the physical path of an image in database - JDBC
how to insert the physical path of an image in database  hello I m... the image is captured it is saved in folder in a project folder and at the same time... path.. and also how to retrive that image path and show that picture in a small
Delete image from folder - JSP-Servlet
Delete image from folder  Hi Thanks Rajnikant to given me reply i am not storing my image in db just userid comming from db means my image name like : Pie(userid).png which alocated in my server folder when
How to make an image full text.
How to make an image full text.     ... an image full text because this tutorial has made it easy. It will provide another option to make an image full text so follow now. Open a file: First open
how to display or retrive an image in jsp whose path is stored in oracle database
how to display or retrive an image in jsp whose path is stored in oracle database  how to display or retrive an image in jsp whose path is stored in oracle database and the image is stored in my pictures folder
How to create file and save it into user defined path using jsp/servlet?
How to create file and save it into user defined path using jsp/servlet?  Hi.. Onclick event I have created one file.When file will create it should asked where to save file(like browse option
Delete image from folder - JSP-Servlet
. that image name based on user id and that image is going to on folder in server after...Delete image from folder  Dear All, i used some coding... creating a new png image. in my project if two user will be login same time meanse
How to get full path of the uploaded file using commons fileupload jar file
How to get full path of the uploaded file using commons fileupload jar file  I want to upload file to database with its full path. Because... type.I am in need to get the full path. Can anyone help me to get the full path
Save profile and image to mysql database, and view the image in another jsp page
Save profile and image to mysql database, and view the image in another jsp page  Pls. need help in saving the profile info with the image in mysql database.. some basic code pls in jsp... thanks in advance
How to copy an jpeg image from one folder to other folder using jsp
How to copy an jpeg image from one folder to other folder using jsp  How to copy an image from one folder to another folder using jsp
Insert image from user using and save in database
Insert image from user using and save in database  when i am trying to upload a image from user and trying to save into oracle9i database...) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) note The full
How to save image after edit in php
How to save image after edit in php  Hello, I have create a simple image editor with the effected of brightness, contrast, and desaturat etc. I have... successfully but i don't know how to replace orignal image with the effects. please
 

 

 

DMCA.com