java上传文件的utils_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;

}

}

来源:https://www.cnblogs.com/youzhongmin/p/5989135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值