Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

jsp multipart/form-data submition

// 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%>" />
Comment

PREVIOUS NEXT
Code Example
Java :: jbutton scroll list full width size 
Java :: Java Enable assertion in package names 
Java :: processing java screen wrap 
Java :: mei mei bad 
Java :: how to find a specific character in a string using array 
Java :: set jtextfield max length 
Java :: Change the java version of a eclips maven project 
Java :: Java Create a FileOutputStream 
Java :: okhttp Synchronous Network Calls 
Java :: java spring crudrepository generate insert instead of update 
Java :: remove activity from recent list android 
Java :: convert text type to int java 
Java :: difference between string vs stringbuffer 
Java :: jdbc outside of ide 
Java :: create a file in java in user home 
Java :: java find nth largest element using priority queue heap 
Java :: Java Insert Elements to EnumSet 
Java :: java code for image encryption & decryption 
Java :: android studio see what activity you came from 
Java :: comvertir a java 
Java :: open application programelly android studio 
Java :: exception(string message throwable cause) 
Java :: Based on the method exampleMethod, what is the return type of the method? 
Java :: list all managed beans in spring 
Java :: como detener un void java 
Java :: JavaFX font display issue 
Java :: reference value in array list java syntax 
Java :: reset android studio settings windows 
Java :: crazy error 
Java :: fibonacci numbers using recursion in java 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =