ImportJava.io.File;ImportJava.util.Scanner;Importorg.apache.commons.io.FileUtils; Public classUploadFile { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); String sourcelocation=Sc.nextline (); String [] Sources= Sourcelocation.split ("\\\\"); intSize =sources.length; String goallocation=NewString ("f:\\ test \ \"); String Goalfilename= Sources[size-1]; Goallocation= Goallocation +Goalfilename; File sourcefile=NewFile (sourcelocation); if(!checkuploadfiletype (Goalfilename,Newstring[]{"PNG"}) {System.out.println ("File format is not correct"); return; } if(!checkuploadfilesize (SourceFile, 1024*1024) {System.out.println ("The file is too large"); return; } Try{File Locationfile=NewFile (goallocation); Fileutils.copyfile (SourceFile, locationfile); }Catch(Exception e) {System.out.println ("Upload Failed"); } } Public Static BooleanCheckuploadfilesize (File file,intsize) {Boolean flag=true; if(File.length () >=size) Flag=false; returnFlag; } Public Static BooleanCheckuploadfiletype (String filename,string[] types) {Boolean flag=false; for(String type:types) {if(Filename.tolowercase (). LastIndexOf (type) +type.length () = =filename.length ()) {Flag=true; } } returnFlag; }}
Fileutils class upload file picture