package susu
.it
;
import java
.io
.*
;
import java
.util
.Scanner
;
public class UploadFile {
public static void main(String
[] args
) throws IOException
{
File path
= getPath();
System
.out
.println(path
);
boolean flag
= isExists(path
.getName());
if (flag
) {
System
.out
.println("该用户头像已经存在,不用上传了");
} else {
uoloadFile(path
);
}
}
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")
&& !path
.endsWith(".bmp")) {
System
.out
.println("您录入的不是图片,请重新录入");
continue;
}
File file
= new File(path
);
if (file
.exists() && file
.isFile()) {
return file
;
} else
System
.out
.println("您录入的路径不合法,请重新录入");
}
}
public static boolean isExists
(String path
){
File file
= new File("lib");
String
[] names
= file
.list();
for (String name
:names
){
if (name
.equals(path
)){
return true ;
}
}
return false;
}
public static void uoloadFile
(File path
) throws IOException
{
BufferedInputStream bis
= new BufferedInputStream(
new FileInputStream(path
));
BufferedOutputStream bos
= new BufferedOutputStream(
new FileOutputStream("lib/"+path
.getName()));
int len
;
while ((len
= bis
.read())!=-1){
bos
.write(len
);
}
bis
.close();
bos
.close();
System
.out
.println("上传成功,哦吼吼 ");
}
}
转载请注明原文地址:https://tech.qufami.com/read-29316.html