首页 > 代码库 > FileUtils类上传文件图片
FileUtils类上传文件图片
import java.io.File; import java.util.Scanner; import org.apache.commons.io.FileUtils; public class UploadFile { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String SourceLocation = sc.nextLine(); String [] sources = SourceLocation.split("\\\\"); int size = sources.length; String GoalLocation = new String("F:\\测试\\"); String GoalFileName = sources[size-1]; GoalLocation = GoalLocation + GoalFileName; File sourcefile = new File(SourceLocation); if(!checkUploadfileType(GoalFileName,new String[]{"png"})) { System.out.println("文件格式不对"); return; } if(!checkUploadfileSize(sourcefile, 1024*1024)) { System.out.println("文件太大"); return; } try{ File locationfile = new File(GoalLocation); FileUtils.copyFile(sourcefile, locationfile); }catch(Exception e){ System.out.println("上传失败"); } } public static boolean checkUploadfileSize(File file, int size){ Boolean flag = true; if(file.length()>=size) flag = false; return flag; } public static boolean checkUploadfileType(String Filename,String[] types){ Boolean flag = false; for(String type:types){ if(Filename.toLowerCase().lastIndexOf(type)+type.length() == Filename.length()){ flag = true; } } return flag; } }
FileUtils类上传文件图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。