All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.webapp.UploadedFile Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.241121-RCM
Show newest version
package nosi.core.webapp;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jakarta.servlet.http.Part;

/**
 * Marcel Iekiny
 * Oct 24, 2017
 */
public final class UploadedFile {
	
	private String fileName;
	private String submittedFileName;
	private String type; // Content-Type 
	private String extension;
	private long size; // bytes
	
	private InputStream in;
	
	private UploadedFile() {}
	
	public static UploadedFile getInstance(String name) {
		UploadedFile file = null;
		try {
			Part part = Igrp.getInstance().getRequest().getPart(name);
			file = new UploadedFile();
			file.fileName = part.getName();
			file.submittedFileName = part.getSubmittedFileName();
			file.type = part.getContentType();
			file.extension = "."+file.type.split("/")[1];
			file.size = part.getSize();
			file.in = part.getInputStream();
		}catch(Exception e){
			e.printStackTrace();
		}
		return file;
	}
	
	public static List getInstances() {
		List files = new ArrayList<>();
		try {
			ArrayList list = new ArrayList<>(Igrp.getInstance().getRequest().getParts()); 
			for(Part obj : list) {
				UploadedFile aux = new UploadedFile();
				aux.fileName = obj.getName();
				aux.submittedFileName = obj.getSubmittedFileName();
				//System.out.println(aux.submittedFileName);
				aux.type = obj.getContentType();
				//aux.extension = aux.type.split("/")[1];
				aux.size = obj.getSize();
				aux.in = obj.getInputStream();
				files.add(aux);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return files;
	}
	
	public static List getInstances(String name) {
		List files = getInstances(); 
		files = files.stream().filter(file ->  
			 file.getFileName().equalsIgnoreCase(name) && file.getSubmittedFileName() != null && !file.getSubmittedFileName().isEmpty()
		).toList(); 
		return files;
	}
	//Leak, if used, must be fixed
	
//	public boolean saveAs(String path) {
//		String result = Igrp.getInstance().getServlet().getServletContext().getRealPath(path);
//		File file = new File(result);
//		boolean flag = false;
//		try {
//			file.createNewFile();
//			FileOutputStream out = new FileOutputStream(file);
//			out.write(this.getContent());
//			out.flush();
//			out.close();
//			flag = true;
//		}catch(Exception e) {
//			e.printStackTrace();
//		}
//		return flag;
//	}
	
	public byte[] getContent() {
		BufferedInputStream stream = new BufferedInputStream(in);
		byte []result = new byte[(int) this.size];
		try {
			int x = stream.read(result);
			if(x == -1) result = null;
		} catch (IOException e) {
			e.printStackTrace();
			result = null;
		}finally {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSubmittedFileName() {
		return submittedFileName;
	}

	public void setSubmittedFileName(String submittedFileName) {
		this.submittedFileName = submittedFileName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	@Override
	public String toString() {
		return "UploadedFile [fileName=" + fileName + ", submittedFileName=" + submittedFileName + ", type=" + type
				+ ", extension=" + extension + ", size=" + size + "]";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy