i want source code for uploading ,displaying and downloading image in jsp

Dani AI

Generated

A concise, practical pattern that works reliably with JSP/Servlet apps: accept uploads with a multipart HTML form and a Servlet (Servlet 3+ @MultipartConfig), write the file to a safe folder outside the webroot (or to object storage) and save metadata (id, originalName, contentType, size, path) in a small table. Serve images by a dedicated servlet that streams bytes and sets the correct Content-Type; serve downloads by the same servlet but add a Content-Disposition: attachment header. Thanks to , and for offering help earlier, and to for resource pointers — the short examples below show the minimal, secure pattern.

Upload servlet (minimal):

@MultipartConfig(maxFileSize = 5_000_000)
public class UploadServlet extends HttpServlet {
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Part p = req.getPart("image");
    String original = Paths.get(p.getSubmittedFileName()).getFileName().toString();
    String id = UUID.randomUUID().toString();
    File uploads = new File(getServletContext().getRealPath("/WEB-INF/uploads"));
    uploads.mkdirs();
    File out = new File(uploads, id);
    try (InputStream in = p.getInputStream(); OutputStream os = new FileOutputStream(out)) {
      byte[] buf = new byte[8192]; int r;
      while ((r = in.read(buf)) != -1) os.write(buf,0,r);
    }
    // persist id, original, contentType, size, path to DB
    resp.sendRedirect("list.jsp");
  }
}

Serve / download (streaming):

public class ImageServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String id = req.getParameter("id");
    // lookup metadata by id -> filename, contentType, original
    File f = new File(/*uploads dir*/, filename);
    resp.setContentType(contentType);
    resp.setContentLengthLong(f.length());
    if ("download".equals(req.getParameter("mode")))
      resp.setHeader("Content-Disposition","attachment; filename=\"" + original + "\"");
    try (InputStream in = new FileInputStream(f); OutputStream out = resp.getOutputStream()) {
      byte[] b = new byte[8192]; int n;
      while ((n = in.read(b)) > 0) out.write(b,0,n);
    }
  }
}

Important notes: always validate size and MIME type and verify image bytes (ImageIO.read) to prevent fake uploads; sanitize or avoid using submitted filenames; store uploads outside webroot; use prepared statements for metadata; apply HTTPS and authentication where needed; set Cache-Control and ETags for served images. This pattern balances simplicity, performance and security for typical JSP projects.

Recommended Answers

All 4 Replies

Ok... hold on! I'm on it!

Member Avatar for Member #120589

And me...!

I have family in from out of town for Christmas and big plans today but I will certainly drop everything and get right on that as well.

commented: Getting the band back together as well. +0

Might I suggest Freelancer.com? It's a raw deal for developers but great for people like yourself who need the work done. And if you post this as a job, you're sure to get someone desperate enough to to do it for you.

But if you really want to do it yourself, go and research the following:
File uploading: https://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html
Displaying images: http://stackoverflow.com/questions/17628945/load-an-img-in-a-jsp
Downloading files: https://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.