模拟用户上传头像(I/O流实例,异常抛出实例)
1,新建方法getPath(),获取用户头像所在的路径,对头像图片格式进行判断(.jpg .png),对路径的合法性进行判断。
public static File getPath(){
//提示用户输入正确路径
Scanner sc =new Scanner(System.in);
while(true){
System.out.println("请输入上传图片的路径:");
String path=sc.nextLine();
//判断是否为图片格式
if(!path.endsWith(".jpg")&&!path.endsWith(".png")){
System.out.println("您输入的不是图片,请输入图片!");
continue;
}
File file=new File(path);
//判断文件是否存在,判断文件路径是否正确
if(!file.exists()&&!file.isFile()){
System.out.println("您输入的路径不存在或者文件不存在请重新输入!");
}
else{
return file;
// System.out.println("头像上传成功!"+path);
}
}
}
2,新建方法isExists(),将路径实例化FIle对象,判断头像所在的文件是否已经存在如果存在,则上传失败,如果不存在则上传成功。
public static boolean isExists(String path){
File file=new File("D://c");
String [] list1=file.list();
for(String list:list1){
if(list.equals(path)){
return true;
}
}
return false;
}
3,新建方法upLoad(),模拟用户上传头像(将头像从源文件传输到目的文件)
public static void upLoad(File path) throws IOException{
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(path));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D://c/"+path.getName()));
int ch;
while((ch=bi.read())!=-1){
bo.write(ch);
}
bi.close();
bo.close();
}
4,完整代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
//模拟用户上传头像
public class UpLoad {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File path=getPath();
System.out.println("头像上传成功!"+path);
boolean flag= isExists(path.getName());
if(flag){
System.out.println("头像已存在,头像上传失败!");
}
else{
upLoad(path);
System.out.println("头像不存在,文件上传成功!");
}
}
public static File getPath(){
//提示用户输入正确路径
Scanner sc =new Scanner(System.in);
while(true){
System.out.println("请输入上传图片的路径:");
String path=sc.nextLine();
//判断是否为图片格式
if(!path.endsWith(".jpg")&&!path.endsWith(".png")){
System.out.println("您输入的不是图片,请输入图片!");
continue;
}
File file=new File(path);
//判断文件是否存在,判断文件路径是否正确
if(!file.exists()&&!file.isFile()){
System.out.println("您输入的路径不存在或者文件不存在请重新输入!");
}
else{
return file;
// System.out.println("头像上传成功!"+path);
}
}
}/*
* 图像是否已经存在
* 存在:上传失败
* 不存在:上传成功
*
*/
public static boolean isExists(String path){
File file=new File("D://c");
String [] list1=file.list();
for(String list:list1){
if(list.equals(path)){
return true;
}
}
return false;
}
public static void upLoad(File path) throws IOException{
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(path));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D://c/"+path.getName()));
int ch;
while((ch=bi.read())!=-1){
bo.write(ch);
}
bi.close();
bo.close();
}
}
5,测试结果:
在这里插入图片描述
6,注意事项:
BufferedOutputStream bo=new BufferedOutputStream(new
FileOutputStream**(“D://c/”+path.getName()));**
文件夹后面的斜杠不能省略否则文件会显示已经成功完成,但是文件夹中却没有图片