Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

jsp form upload file

// Press Ctrl+Space on the errors to insert required imports
//More here: https://commons.apache.org/proper/commons-fileupload/using.html
String img_url = null;

if (ServletFileUpload.isMultipartContent(request)) {

  InputStream is = null;
  String line = null;

  DiskFileItemFactory factory = new DiskFileItemFactory();

  // Configure a repository (to ensure a secure temp location is used)
  ServletContext servletContext = this.getServletConfig().getServletContext();
  File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
  factory.setRepository(repository);

  // Create a new file upload handler
  ServletFileUpload upload = new ServletFileUpload(factory);

  // Parse the request
  List<FileItem> items = upload.parseRequest(new ServletRequestContext(request));
  Iterator<FileItem> iter = items.iterator();

  while (iter.hasNext()) {
    FileItem item = iter.next();

    if (item.isFormField()) {
  			out.print(item.getString());
       		out.print(item.getFieldName());
    } else {
      byte[] data = item.get();
      String imageStr = Base64.getEncoder().encodeToString(data);
      out.print(imageStr);		
      img_url= imageStr;
    }

  }
Then display image as: <img alt="img" src="data:image/png;base64,<%=img_url%>" />
Source by localhost #
 
PREVIOUS NEXT
Tagged: #jsp #form #upload #file
ADD COMMENT
Topic
Name
2+7 =