package org.theonefx.wcframework.mvc.wcweb;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.theonefx.wcframework.utils.FileUtils;
import org.theonefx.wcframework.utils.StringUtils;
public class UploadFile {
private String fileName;
private String contentType;
private File file;
private long size;
private String ext;
public UploadFile(String fileName, String contentType, File file, long size) {
this.fileName = StringUtils.trimAllWhitespace(fileName);
this.file = file;
this.contentType = contentType;
this.size = size;
if (StringUtils.isNotBlank(fileName)) {
ext = FileUtils.getSuffixName(fileName);
}
}
public String getFileName() {
return fileName;
}
public String getContentType() {
return contentType;
}
public long getSize() {
return size;
}
public File getFile() {
return file;
}
public String getExt() {
return ext;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
file.delete();
}
public boolean saveAs(String path) throws IOException {
if (file == null || !file.exists()) {
return false;
}
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1000];
int length = -1;
while ((length = fis.read(buffer)) > -1) {
bos.write(buffer, 0, length);
}
try {
fis.close();
} finally {
bos.flush();
bos.close();
}
return true;
}
public InputStream getInputStream() throws FileNotFoundException {
if (file == null || !file.exists()) {
return null;
}
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bos = new BufferedInputStream(fis);
return bos;
}
public boolean exist() {
if (file == null) {
return false;
}
if (!file.exists()) {
return false;
}
return true;
}
}