栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现上传文件到本地服务器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现上传文件到本地服务器

public static void main(String[] args) throws IOException {
		//目标路径
		String filePath = "E:"+File.separator+"test"+File.separator;
		File file = new File(filePath);
		//如果文件目录不存在,就执行创建		
		if(!file.isDirectory()){
			file.mkdirs();
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHss");
		//目标文件名称
		String targetName =sdf.format(new Date()) + ".jpg";
		System.out.println(targetName);
		//创建目标文件
		File targetFile = new File(filePath + targetName);
		FileOutputStream fos = new FileOutputStream(targetFile);
		
		//读取本地文件
		File localFile = new File("E:"+File.separator+"1.jpg");
		//获取本地文件输入流
		InputStream stream = new FileInputStream(localFile);
		
		//写入目标文件
		byte[] buffer = new byte[1024*1024];
		int byteRead = 0;
		while((byteRead=stream.read(buffer))!=-1){
			fos.write(buffer, 0, byteRead);
			fos.flush();
		}
		fos.close();
		stream.close();
	}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/238941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号