Java文件上传下载

文件上传下载

Java文件上传和下载对于刚接触Java没多久的老铁们来说可能是一个技术难点。如果看过我前两篇文章的老铁肯定就知道,这次肯定又是一个工具类,废话少说我们直接附上代码。

package com.fashion.fox.ctr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.fashion.fox.ctr.Constant;

/**
 * TODO:文件上传和下载
 * @Title Utils 工具类
 * @author Mr.Zhang  
 * @date 2019年10月18日  
 * @version 1.0
 */
@Component
public class LoadUtils {
	/**
	 * TODO:文件下载
	 * @param urlStr
	 * @param savePath
	 * @return boolean
	 */
	public boolean downLoadFromUrl(String urlStr, String savePath) {
		OutputStream os = null;
		InputStream inputStream = null;
		try {
			URL url = new URL(urlStr);
			//这一步骤的目的是判别文件的后缀名
			String end = urlStr.trim();
			for (int i = 0; i < urlStr.length(); i++) {
				int index = end.indexOf(".");
				if (index < 0) {
					break;
				}
				end = end.substring(index + 1);
			}
			String fileName = UUID.randomUUID().toString() + "." + end;
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置超时间为3秒
			conn.setConnectTimeout(3 * 1000);
			// 防止屏蔽程序抓取而返回403错误
			conn.setRequestProperty(Constant.USERAGENT, Constant.MOZILLA);
			// 得到输入流
			inputStream = conn.getInputStream();
			// 获取自己数组
			byte[] bs = new byte[1024];
			int len;
			// 文件保存位置
			File tempFile = new File(savePath);
			if (!tempFile.exists()) {
				tempFile.mkdirs();
			}
			os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
			// 开始读取
			while ((len = inputStream.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			// 完毕,关闭所有链接
			try {
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * TODO:文件上传
	 * @param file
	 * @param FilePath
	 * @param FileName
	 * @return boolean
	 */
	public boolean upLoad(MultipartFile file, String savePath, String FileName) {
		OutputStream os = null;
		InputStream inputStream = null;
		String fileName = null;
		try {
			String imgeAfter = file.getOriginalFilename();
			//获取文件后缀名
			imgeAfter = imgeAfter.substring(imgeAfter.indexOf("."));
			inputStream = file.getInputStream();
			//拼接文件上传路径
			fileName = FileName + imgeAfter;
			// 2、保存到临时文件
			// 1K的数据缓冲
			byte[] bs = new byte[1024];
			// 读取到的数据长度
			int len;
			// 输出的文件流保存到本地文件
			File tempFile = new File(savePath);
			if (!tempFile.exists()) {
				tempFile.mkdirs();
			}
			os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
			// 开始读取
			while ((len = inputStream.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			// 完毕,关闭所有链接
			try {
				os.close();
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;

	}
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值