Package com.zero4j.util;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.context.ServletContextAware;
Import Org.springframework.web.multipart.MultipartFile;
/**
*
* Upload Tool class
*
*
*/
@Component
public class Uploadutil implements Servletcontextaware {
private static ServletContext ServletContext = null;
@Override
public void Setservletcontext (ServletContext servletcontext) {
This.servletcontext = ServletContext;
}
/**
* Save File
*
* @param files
* @param ID
* @param request
* @author HZR December 5, 2016
*/
public static void FileSave (multipartfile files, String ID, httpservletrequest request) {
String filefilename = Files.getoriginalfilename ();
if (files = = NULL | | files.equals (")") {
Return
} else {
String Extension = (filefilename.substring (Filefilename.lastindexof (".") + 1, filefilename.length ())). toLowerCase (); Get extension and convert to lowercase
Start the upload operation
try {
Set a temporary directory for image uploads
File Srcfile = new file (Servletcontext.getrealpath ("/uploads/other/"), ID + "." + extension);
System.out.println (Srcfile);
if (!srcfile.exists ()) {
SYSTEM.OUT.PRINTLN ("Path does not exist!!!!!!!! ");
Srcfile.mkdirs ();
}
Files.transferto (Srcfile);
} catch (Exception e) {
E.printstacktrace ();
Return
}
/*
*//If the file suffix is different, delete the original file first
* IF (files.getextension () ==null| |! Extension.equals
* (_upload.getextension (). toLowerCase ())) {New
* File (Servletactioncontext
*. getrequest (). Getrealpath ("/uploads/other"),
* _upload.getid () + "." +_upload.getextension ()). Deleteonexit (); }
*
*//update file suffix to database _upload.setcontenttype (This.filecontenttype);
* _upload.setextension (extension);
* This.uploadService.update (_upload);
*/
// }
}
}
/**
* Save File
*
* @param files
* @param ID
* @param request
* @author HZR 20 December 5, 16
*/
public static string Filesave_other (multipartfile files, String ID, httpservletrequest request) {
String filefilename = Files.getoriginalfilename ();
if (files = = NULL | | files.equals (")) {
return null;
} else {
String extension = (filefilename.substring (Filefilename.lastindexof (".") + 1, filefilename.length ())). toLowerCase (); Get extension and convert to lowercase
//start upload operation
try {
//Set image upload temp directory
File Srcfile = new file (Servletcontext.getrealpath ("/uploads/styleitem/"), ID + "." + Extension );
System.out.println (srcfile);
if (!srcfile.exists ()) {
System.out.println ("The file path does not exist!!!!!!!! ");
Srcfile.mkdirs ();
}
/*string imagesourcepath = Request.getrealpath ("/uploads/styleitem");
File Srcfile = new file (Imagesourcepath, id + "." + extension);
if (!srcfile.exists ()) {
Srcfile.mkdir ();
}*/
//system.out.println (srcfile.tostring () + "path Asdasdasdsa");
Files.transferto (Srcfile);
return "/uploads/styleitem/" +id + "." + extension;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
/*
*//If the file suffix is different, delete the original file first
* IF (files.getextension () ==null| |! Extension.equals
* (_upload.getextension (). toLowerCase ())) {New
* File (Servletactioncontext
*. getrequest (). Getrealpath ("/uploads/other"),
* _upload.getid () + "." +_upload.getextension ()). Deleteonexit (); }
*
*//update file suffix to database _upload.setcontenttype (This.filecontenttype);
* _upload.setextension (extension);
* This.uploadService.update (_upload);
*/
// }
}
}
/**
* Delete Folder
* @param filepathandname String folder path and name such as C:/FQF
* @param filecontent String
* @return Boolean
*/
public static void Delfolder (String folderpath) {
Folderpath=servletcontext.getrealpath (FolderPath);
System.out.println ("Execute Delete folder operation source file path" +folderpath);
try {
Delallfile (FolderPath); Delete all content inside
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
Java.io.File Myfilepath = new Java.io.File (FilePath);
Myfilepath.delete (); Delete Empty folders
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error deleting folder operation");
E.printstacktrace ();
}
}
/**
* Delete all files inside the folder
* @param path String folder paths such as C:/fqf
*/
public static void Delallfile (String path) {
Path=servletcontext.getrealpath (path);
File File = new file (path);
if (!file.exists ()) {
Return
}
if (!file.isdirectory ()) {
Return
}
string[] templist = File.list ();
File temp = null;
for (int i = 0; i < templist.length; i++) {
if (Path.endswith (File.separator)) {
temp = new File (path + templist[i]);
}
else {
temp = new File (path + file.separator + templist[i]);
}
if (Temp.isfile ()) {
Temp.delete ();
}
if (Temp.isdirectory ()) {
Delallfile (path+ "/" + templist[i]);//delete the files in the folder first
Delfolder (path+ "/" + templist[i]);//delete empty folders again
}
}
}
/**
* Copy individual files
* @param oldpath String Original file relative path such as: C:/fqf.txt
* @param newpath String After copying relative paths such as: F:/fqf.txt
* @return Boolean
*/
public static Boolean CopyFile (String OldPath, String NewPath) {
System.out.println ("Execute Copy file operation source file path" +servletcontext.getrealpath (OldPath) + "Destination file path" +servletcontext.getrealpath ( NewPath));
try {
int bytesum = 0;
int byteread = 0;
File Oldfile = new file (Servletcontext.getrealpath (OldPath));
File Srcfile = new file (Servletcontext.getrealpath ("/uploads/styleitemtemplate/"));
System.out.println (Srcfile);
if (!srcfile.exists ()) {
System.out.println ("The folder path does not exist!!!!!!!! ");
Srcfile.mkdirs ();
}
File myFileName = new file (Servletcontext.getrealpath (NewPath));
try{
if (!myfilename.exists ()) {
Myfilename.createnewfile ();
}
}catch (Exception e) {
System.out.println ("New file Operation Error");
E.printstacktrace ();
return false;
}
if (oldfile.exists ()) {//File exists
InputStream instream = new FileInputStream (Servletcontext.getrealpath (OldPath)); Read in the original file
FileOutputStream fs = new FileOutputStream (Servletcontext.getrealpath (NewPath));
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))! =-1) {
Bytesum + = Byteread; Number of bytes File size
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
System.out.println ("copy succeeded");
}
return true;
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error copying Single file operation");
E.printstacktrace ();
return false;
}
}
/**
* Copy individual files
* @param tempaltefilefolderroute String temporary folder path relative path such as: C:/FQF
* @param newpath String Resource folder path relative path such as: F:/fqf1
* @return Boolean
*/
public static Boolean Copyfilefolder (String Tempaltefilefolderroute, String NewPath) {
System.out.println ("Execute Copy file operation source file path" +servletcontext.getrealpath (OldPath) + "Destination file path" +servletcontext.getrealpath ( NewPath));
try {
File Srcfile = new file (Servletcontext.getrealpath (Tempaltefilefolderroute));
if (!srcfile.exists ()) {
System.out.println ("The folder path does not exist!!!!!!!! ");
Srcfile.mkdirs ();
}
File SrcFile1 = new file (Servletcontext.getrealpath (NewPath));
if (!srcfile1.exists ()) {
System.out.println ("The folder path does not exist!!!!!!!! ");
Srcfile1.mkdirs ();
}
String Path=servletcontext.getrealpath (Tempaltefilefolderroute);
File File = new file (path);
string[] templist = File.list ();
File temp = null;
for (int i = 0; i < templist.length; i++) {
String temproute=tempaltefilefolderroute+ "/" +templist[i];
String normalroute=newpath+ "/" +templist[i];
File Oldfile = new file (Servletcontext.getrealpath (Temproute));
File myFileName = new file (Servletcontext.getrealpath (Normalroute));
int byteread = 0;
try{
if (!myfilename.exists ()) {
Myfilename.createnewfile ();
}
}catch (Exception e) {
System.out.println ("New file Operation Error");
E.printstacktrace ();
return false;
}
if (oldfile.exists ()) {//File exists
InputStream instream = new FileInputStream (Servletcontext.getrealpath (Temproute)); Read in the original file
FileOutputStream fs = new FileOutputStream (Servletcontext.getrealpath (Normalroute));
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))! =-1) {
Bytesum + = Byteread; Number of bytes File size
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
System.out.println ("copy succeeded");
}
}
return true;
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error copying file operation");
E.printstacktrace ();
return false;
}
}
/*para
Source file directory
Dest Copying Files directory
para*/
/* Copy files */
public static void CopyFile2 (string source, String dest) {
System.out.println ("Execute Copy file operation source file path" +servletcontext.getrealpath (source) + "Destination file path" +servletcontext.getrealpath (dest ));
File Srcfile = new file (Servletcontext.getrealpath ("/uploads/styleitemtemplate"));
System.out.println (Srcfile);
if (!srcfile.exists ()) {
System.out.println (Servletcontext.getrealpath (dest) + "path does not exist!!!!!!!! ");
Srcfile.mkdirs ();
}
try {
File in = new file (Servletcontext.getrealpath (source));//Source Directory
File out = new file (Servletcontext.getrealpath (dest)); Copy Files directory
FileInputStream inFile = new FileInputStream (in);
FileOutputStream outFile = new FileOutputStream (out);
byte[] buffer = new byte[102400];
int i = 0;
while ((i = infile.read (buffer))! =-1) {
Outfile.write (buffer, 0, I);
}//end while
Infile.close ();
Outfile.close ();
System.out.println ("copy succeeded");
}//end try
catch (Exception e) {
System.out.println (E.tostring ());
}//end catch
}//end CopyFile
/* Delete File Method */
public boolean DeleteFile (String spath) {
System.out.println ("Execute Delete file operation source file path" +servletcontext.getrealpath ("/uploads/styleitem") +spath);
Boolean flag = false;
File File = new file (Servletcontext.getrealpath ("/uploads/styleitem") +spath);
The path is a file and is not empty to delete
if (File.isfile () && file.exists ()) {
File.delete ();
Flag = true;
}
return flag;
}
}
Java file processing