Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Java :: Janaaa_Sa 
Java :: random number in java between 10 and 20 
Java :: java dictionary array initialization 
Java :: search for a string in byte array 
Java :: at org.apache.spark.sql.errors.QueryCompilationErrors$.failedToFindKafkaDataSourceError(QueryCompilationErrors.scala:1037) 
Java :: pgzint install windows 10 
Java :: recursive in java 
Java :: @android:color/system_neutral1_1000 
Java :: open google maps cycling navigation 
Java :: spring mvc aop transaction management 
Java :: bloomreach get node via id 
Java :: arraylistof objects 
Java :: Bukkit dev paid account 
Java :: react native appearance.addchangelistener android 
Java :: how to declare and allocate memory to array in java 
Java :: java pattern matching 
Java :: short array in java 
Java :: quadratic program 
Java :: lighting strike solved 
Java :: spring converter in initbinder 
Java :: doubly linked list java add an element to the end 
Java :: how to create a 2d arraylist java 
Java :: method in java 
Java :: java join list 
Java :: java resto 
Java :: java file path linux 
Java :: maven set repository location command line 
Java :: int in string umwandeln 
Java :: matlab leslie eigenvalue java 
Sql :: delete mysql ubuntu 20.04 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =