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 :: print map in java log 
Java :: android studio enum usage 
Java :: castle stones java qn 
Java :: buat tetris dari java 
Java :: find largest number in 2d array java 
Java :: customize the androidx.appcompat.widget.SearchView site:stackoverflow.com 
Java :: sphere 
Java :: grunt registertask multiple 
Java :: custom class level annotation in spring 
Java :: java get end of date 
Java :: is java good then c++ is good 
Java :: electron driver window maximize selenium webdriver 
Java :: disarium number in java 
Java :: fibonacci numbers using recursion in java 
Java :: como codificar pilas en java creeper 
Java :: javafx check if enter pressed 
Java :: java private access modifier 
Java :: how do you rate yourself in java 
Java :: Java 17 not showing in linux 
Java :: minecraft bukkit coding player sharing variable 
Java :: how to fetch email and get there body content in spring boot 
Java :: Reference in java equal operator 
Java :: switch expression 
Java :: httpsession vs cookie servlet 
Java :: also in java 
Java :: clear method does not work selenium java 
Java :: Deal with empty or blank cell in excel file using apache poi 
Java :: how to change text of textview in slide menu bar header android 
Java :: how to add a command to a button java 
Java :: how to check if parsing in integer is possible in java 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =