package com.sixfacet.framework.web.util.file;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
public class FileUtil {
public static List<MultipartFile> getFiles(HttpServletRequest request) throws Exception {
MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
MultipartFile file = null;
List<MultipartFile> files = new ArrayList<MultipartFile>();
for(Object o: req.getFileMap().values()) {
if(o != null) {
file = (MultipartFile)o;
files.add(file);
}
}
return files;
}
public static void saveXmlToFile(String xml, String fileName) throws IOException{
FileWriter outFile = new FileWriter(fileName);
PrintWriter out = new PrintWriter(outFile);
try {
out.write(xml);
} finally {
out.close();
outFile.close();
}
}
public static String readXmlFromFile(String fileName) throws IOException{
//create FileInputStream object
FileInputStream fin = new FileInputStream(fileName);
//create object of BufferedInputStream
BufferedInputStream bin = new BufferedInputStream(fin);
String strFileContents = "";
try {
//create a byte array
byte[] contents = new byte[1024];
int bytesRead=0;
while( (bytesRead = bin.read(contents)) != -1){
strFileContents += new String(contents, 0, bytesRead);
}
} finally {
bin.close();
fin.close();
}
return strFileContents;
}
public static void copyfile(File srFile, File dtFile) throws IOException{
InputStream in = new FileInputStream(srFile);
//For Append the file.
//OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(dtFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
}
public static void deletefile(File file) throws IOException{
// Make sure the file or directory exists and isn't write protected
if (!file.exists())
throw new IllegalArgumentException(
"Delete: no such file or directory: " + file.getName());
if (!file.canWrite())
throw new IllegalArgumentException("Delete: write protected: "
+ file.getName());
// If it is a directory, make sure it is empty
if (file.isDirectory()) {
String[] files = file.list();
if (files.length > 0)
throw new IllegalArgumentException(
"Delete: directory not empty: " + file.getName());
}
// Attempt to delete it
boolean success = file.delete();
if (!success)
throw new IllegalArgumentException("Delete: deletion failed");
}
/**
* Get File Type for a given MultipartFile
* @param attachedFile
* @return
*/
public static String getFileType(MultipartFile attachedFile){
return getFileType(attachedFile.getOriginalFilename());
}
/**
* Get File Type for a given File
* @param attachedFile
* @return
*/
public static String getFileType(File attachedFile){
return getFileType(attachedFile.getName());
}
/**
* Get File Type for a given filename
* If a file extension is not recognized, application/octet-stream is assumed
* @param filename
* @return
*/
public static String getFileType(String filename){
if (filename.endsWith(".PDF") || filename.endsWith(".pdf")){
return "application/pdf";
} else if (filename.endsWith(".doc") || filename.endsWith(".DOC")){
return "application/msword";
} else if (filename.endsWith(".xls") || filename.endsWith(".XLS")){
return "application/msexcel";
}else{
return "application/octet-stream";
}
}
public static void saveBytesToFile(byte[] fileDate, File file) throws IOException{
FileOutputStream outFile = new FileOutputStream(file);
outFile.write(fileDate);
outFile.close();
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
}
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
public class FileUtil {
public static List<MultipartFile> getFiles(HttpServletRequest request) throws Exception {
MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
MultipartFile file = null;
List<MultipartFile> files = new ArrayList<MultipartFile>();
for(Object o: req.getFileMap().values()) {
if(o != null) {
file = (MultipartFile)o;
files.add(file);
}
}
return files;
}
public static void saveXmlToFile(String xml, String fileName) throws IOException{
FileWriter outFile = new FileWriter(fileName);
PrintWriter out = new PrintWriter(outFile);
try {
out.write(xml);
} finally {
out.close();
outFile.close();
}
}
public static String readXmlFromFile(String fileName) throws IOException{
//create FileInputStream object
FileInputStream fin = new FileInputStream(fileName);
//create object of BufferedInputStream
BufferedInputStream bin = new BufferedInputStream(fin);
String strFileContents = "";
try {
//create a byte array
byte[] contents = new byte[1024];
int bytesRead=0;
while( (bytesRead = bin.read(contents)) != -1){
strFileContents += new String(contents, 0, bytesRead);
}
} finally {
bin.close();
fin.close();
}
return strFileContents;
}
public static void copyfile(File srFile, File dtFile) throws IOException{
InputStream in = new FileInputStream(srFile);
//For Append the file.
//OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(dtFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
}
public static void deletefile(File file) throws IOException{
// Make sure the file or directory exists and isn't write protected
if (!file.exists())
throw new IllegalArgumentException(
"Delete: no such file or directory: " + file.getName());
if (!file.canWrite())
throw new IllegalArgumentException("Delete: write protected: "
+ file.getName());
// If it is a directory, make sure it is empty
if (file.isDirectory()) {
String[] files = file.list();
if (files.length > 0)
throw new IllegalArgumentException(
"Delete: directory not empty: " + file.getName());
}
// Attempt to delete it
boolean success = file.delete();
if (!success)
throw new IllegalArgumentException("Delete: deletion failed");
}
/**
* Get File Type for a given MultipartFile
* @param attachedFile
* @return
*/
public static String getFileType(MultipartFile attachedFile){
return getFileType(attachedFile.getOriginalFilename());
}
/**
* Get File Type for a given File
* @param attachedFile
* @return
*/
public static String getFileType(File attachedFile){
return getFileType(attachedFile.getName());
}
/**
* Get File Type for a given filename
* If a file extension is not recognized, application/octet-stream is assumed
* @param filename
* @return
*/
public static String getFileType(String filename){
if (filename.endsWith(".PDF") || filename.endsWith(".pdf")){
return "application/pdf";
} else if (filename.endsWith(".doc") || filename.endsWith(".DOC")){
return "application/msword";
} else if (filename.endsWith(".xls") || filename.endsWith(".XLS")){
return "application/msexcel";
}else{
return "application/octet-stream";
}
}
public static void saveBytesToFile(byte[] fileDate, File file) throws IOException{
FileOutputStream outFile = new FileOutputStream(file);
outFile.write(fileDate);
outFile.close();
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
}