1

How can I upload an image using simply JSP and Servlet. I used as below code to get an image in JSP page:

<p>File: <input type="file" name="file_to_upload"> </p>

Now I would like to know how to get these files and how to save them in specific directory ? I named that folder "images", and I would like to put all files these I uploaded in that folder. The images would actually be saved when I restart my server or refresh my page.

How can I achieve this?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Paradoxianus
  • 11
  • 1
  • 1
  • 2
  • 1
    have you tried with somecodes or find in Google or in SO ? I think there has many sample useful codes. Or post your codes these had you tried. – Cataclysm Feb 20 '14 at 11:52

3 Answers3

2

To upload a file using JSP, you have to deal with a multipart/form-data request.

The easiest way I know to do that is using apache commons fileupload as described in details here http://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html

ps. the tutorial is a little bit wrong :-)

the index.jsp must contain something like this

<form action="upload" method="post" name="uploadForm" enctype="multipart/form-data">
<input name="uploadfile" type="file" size="50″>
<input name="submit" type="submit" value="Submit">
</form>
Leo
  • 6,480
  • 4
  • 37
  • 52
  • Alternatively, it is also possible without additional dependencies in servlet spec 3.0. http://balusc.blogspot.nl/2009/12/uploading-files-in-servlet-30.html – Gimby Feb 20 '14 at 12:09
  • It took a long time but finally they've added to the servlet spec! thanks for the tip! – Leo Feb 20 '14 at 12:25
  • 1
    I'll rephrase your comment: "They should have added it a decade ago ago!". And then we can all agree. – Gimby Feb 20 '14 at 12:36
  • You're a gentleman. thanks ;-) – Leo Feb 20 '14 at 12:38
1

the form which used upload file must hava ' enctype="multipart/form-data" '.

<%@ page language="java"  pageEncoding="gb2312"%>

<%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>

<%@ page import="mainClass.*" %>

<html>
  <head>
   <title>My JSP 'uploadimage.jsp' starting page</title>
  </head>
  <body>

    <%

    SmartUpload sma=new SmartUpload();
    long file_max_size=4000000;
    String filename1="",ext="",testvar="";
    String url="uploadfiles/";
    sma.initialize(pageContext);
     try

    {

    sma.setAllowedFilesList("jpg,gif");
    sma.upload();
    }catch(Exception e){

    %>

    <script language="jscript">

    alert("only jpg,gif allowed to upload!")

    window.location.href="upfile.jsp"

    </script>

    <%



     }

        try{

        com.jspsmart.upload.File myf=sma.getFiles().getFile(0);

        }else{
        ext=myf.getFileExt();

        int file_size=myf.getSize();
        String saveurl="";
        if(file_size < file_max_size){
        Calendar cal=Calendar.getInstance();
        String filename=String.valueOf(cal.getTimeInMillis());
        saveurl=request.getRealPath("/")+url;
        saveurl+=filename+"."+ext;
        myf.saveAs(saveurl,sma.SAVE_PHYSICAL

);
    myclass mc=new myclass(request.getRealPath("data/data.mdb"));
      mc.executeInsert("insert into [path] values('uploadfiles/"+filename+"."+ext+"')");    out.println("ok!");
    response.sendRedirect("showimg.jsp");
    }
    }

    }catch(Exception e){

    e.printStackTrace();

    }
    %>
  </body>
</html>

when you want to display you file ,you should write your filepath in your project ,so don't save your file at other place,it will make trouble when your server want to find it.

first answer question ,so you understand...

ying
  • 11
  • 1
0

I think that the best way to deal with file upload (and multipart/form-data requests) is using Apache common file upload

It offers many useful classes to help in managing file upload matters.

cigno5.5
  • 703
  • 4
  • 13