Horje
jsp form upload image Code Example
jsp form upload image
// 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: localhost




Java

Related
spring cli version Code Example spring cli version Code Example
what is datasnapshot.getkey() in android studio Code Example what is datasnapshot.getkey() in android studio Code Example
public class App{     public static void main(string [] args){         System.out.println("   /|");         System.out.println("  / |");         System.out.println(" /  |");         System.ou public class App{ public static void main(string [] args){ System.out.println(" /|"); System.out.println(" / |"); System.out.println(" / |"); System.ou
how to change state of a Switch programmatically andoir dstudio Code Example how to change state of a Switch programmatically andoir dstudio Code Example
Java program pattern program to triangle using 100 numbers Code Example Java program pattern program to triangle using 100 numbers Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7