如何实现文件的上传
Posted 雷超朝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现文件的上传相关的知识,希望对你有一定的参考价值。
package upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 将c:\\a.doc的文件复制到d:\\a.doc
*/
public class Test {
public static void main(String[] args) throws Exception {
getInputStream("c://a.doc", "d://a.doc");
}
private static void getInputStream(String pathName,String copyName) throws FileNotFoundException, IOException{
File file = new File(pathName);
if(!file.exists()){
throw new RuntimeException("文件不存在");
}else{
getCopy(copyName, new BufferedInputStream(new FileInputStream(file)));
}
}
private static void getCopy(String copyName,BufferedInputStream bis) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyName));
BufferedInputStream biss=bis;
byte[] b = new byte[biss.available()];
int len = 0;
while((len=biss.read(b))!=-1){
bos.write(b, 0, len);
}
bos.close();
biss.close();
System.out.println(copyName+"复制成功!");
}
}
以上是关于如何实现文件的上传的主要内容,如果未能解决你的问题,请参考以下文章