4

My goal is to upload an image and save inside images folder to Web Content Folder.

{Please see the image below}

enter image description here

and save the image path into database

{Please see the image below}

enter image description here

I encountered some problem at this link I tried to do but I did not managed to save the image to images/users folder.

And I realize that the save path is store at my C DRIVE. What if I change my computer? Will the image will be there?

Below are my codes. Help will be appreciate.. Thanks! :)

In jsp

<input type="file" name="file">

In servlet

public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String SAVE_DIR = "WebContent\\images\\users";

.........

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

     // gets absolute path of the web application
        String appPath = request.getServletContext().getRealPath("");
        // constructs path of the directory to save uploaded file
        String savePath = appPath + File.separator + SAVE_DIR;

        // creates the save directory if it does not exists
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdir();
        }

        for (Part part : request.getParts()) {
            String fileName = extractFileName(part);
            part.write(savePath + File.separator + fileName);
        }

System.out.println(savePath);

}

       private String extractFileName(Part part) {
            String contentDisp = part.getHeader("content-disposition");
            String[] items = contentDisp.split(";");
            for (String s : items) {
                if (s.trim().startsWith("filename")) {
                    return s.substring(s.indexOf("=") + 2, s.length()-1);
                }
            }
            return "";
        }
Purple Owl
  • 137
  • 1
  • 6
  • 19
  • what have you tried? bdw,its completely up to you and your requirement and i haven't seen any disadvantages doing as such. – Sasi Kathimanda Dec 31 '13 at 02:52
  • @SasiKathimanda Basically, I have not start yet because I am not sure how to do. Do you have any idea how to do? :) – Purple Owl Dec 31 '13 at 03:02
  • 1
    take a look at this, http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api .and all what you need to do is, get fileuploaded file location path and save to db. – Sasi Kathimanda Dec 31 '13 at 03:07
  • @SasiKathimanda Hey, thanks for the link! Unfortunately, I did not managed to upload into my folder.. Please take a look at my EDITED question. Thanks! :) – Purple Owl Dec 31 '13 at 14:19

1 Answers1

0

Follow the link provided by SasiKathimanda for code implementation.

Create the file name as 'OrignalFilename+SystemTimeInMills' in order to avoid overriding already uploaded file with same filename.

Otherwise check for duplicate filename before write filecontent to server.

Follow the pattern(For DB File Path) if you need to provide download option(later), otherwise you can save the uploaded absolute file path.

  • Panneersel Hey, thanks! Now I know how to save in database by getting the absolute file path. However, I did not managed to upload into my folder. Please take a look at my EDITED question. Thanks! :) – Purple Owl Dec 31 '13 at 14:21
  • U need to create the required folder to save before u deploy your application. Always try to get the server (ie tomcat server location) root folder and store. – Sureshkumar Panneerselvan Jan 01 '14 at 15:23
  • Panneerselvan I already create the folder. Please refer to the first image. I am trying to save at webcontent. Must I save at server root? Sorry I am new in this. – Purple Owl Jan 02 '14 at 07:13
  • Yes u should do at server, because if webcotent will be deleted upon new war deployment. – Sureshkumar Panneerselvan Jan 02 '14 at 07:15
  • Ok, now I understand why there is a need to save at server. Thanks for explaining to me.The savepath is C:\Users\....\Desktop\...\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyProjectName\ – Purple Owl Jan 02 '14 at 15:20
  • Is this my tomcat server root? Sorry I am totally a beginner. – Purple Owl Jan 02 '14 at 15:25
  • Not required, write the code to create the folder in server location apart from server web deployement folder.. – Sureshkumar Panneerselvan Jan 03 '14 at 01:53