Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload image and Save inside Web Content Folder in JSP and Save Image Path in Database
    primarykey
    data
    text
    <p>My goal is to upload an image and save inside images folder to Web Content Folder.</p> <p>{Please see the image below}</p> <p><img src="https://i.stack.imgur.com/LGo0K.jpg" alt="enter image description here"></p> <p>and save the image path into database</p> <p>{Please see the image below}</p> <p><img src="https://i.stack.imgur.com/tauXq.jpg" alt="enter image description here"></p> <p>I encountered some problem at <a href="http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api" rel="nofollow noreferrer">this link</a> I tried to do but I did not managed to save the image to images/users folder.</p> <p>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?</p> <p>Below are my codes. Help will be appreciate.. Thanks! :)</p> <p>In <strong>jsp</strong></p> <pre><code>&lt;input type="file" name="file"&gt; </code></pre> <p>In <strong>servlet</strong></p> <pre><code>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 ""; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload