java下载网络文件并重命名

文件下载,通过<a href="url">也是可以的,但是这样直接下载,一般文件名就是服务器端的没有任何意义的文件名。

今天自己用到了另外一种,先说需求:1.文件服务器与系统没有在同一服务器,所以需要使用网络地址来进行下载;

2.上传时为了避免文件重名,使用uuid来生成了文件名,真实的文件名存与数据库中;

3.所有的文件都只能下载,不可直接在浏览器上打开。

根据代码来分析:这里使用的springmvc

@RequestMapping("/download")
	public String downloadAmachment(String downloadUrl, String realFileName, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/html;charset=UTF-8");
		try {
			request.setCharacterEncoding("UTF-8");

			BufferedInputStream bis = null;
			BufferedOutputStream bos = null;
			//此处使用的配置文件里面取出的文件服务器地址,拼凑成完整的文件服务器上的文件路径
                        //写demo时,可以直接写成http://xxx/xx/xx.txt.这种形式
                        String downLoadPath = ConfigHelper.getString("img.server.url") + downloadUrl;

			response.setContentType("application/octet-stream");
			response.reset();//清除response中的缓存
			//根据网络文件地址创建URL
                        URL url = new URL(downLoadPath);
                        //获取此路径的连接
                        URLConnection conn = url.openConnection();
			
                        Long fileLength = conn.getContentLengthLong();//获取文件大小
			//设置reponse响应头,真实文件名重命名,就是在这里设置,设置编码
                        response.setHeader("Content-disposition",
					"attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(fileLength));

			bis = new BufferedInputStream(conn.getInputStream());//构造读取流
			bos = new BufferedOutputStream(response.getOutputStream());//构造输出流
			byte[] buff = new byte[1024];
			int bytesRead;
			//每次读取缓存大小的流,写到输出流
                        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
			response.flushBuffer();//将所有的读取的流返回给客户端
			bis.close();
			bos.close();

		} catch (IOException e) {
			LOG.error(e.getMessage(), e);
			return ErrorPages._500;
		}
		return null;
	}


前台页面可以用一个<a href="/download?downloadUrl=xxxxx&realFileName=yyyy">这里的xxxxx为文件的网络地址,yyy为文件的真实具有意义的文件名。

这种做法,主要是针对不同服务器上,不能直接通过磁盘盘符例如:D:/xx/xx.txt这种形式来构建File来进行下载。同时,生产系统,文件服务器万一更改了,到时候还需要直接修改代码,维护性不高;同时解决文件下载下来,得到的是有具体意义的文件名。


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用阿里云 Java SDK 中的 `ObsClient` 类来下载文件重命名。以下是示例代码: ```java import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.SSECustomerKey; import java.io.File; import java.io.IOException; public class ObsDownloadAndRenameDemo { public static void main(String[] args) throws IOException { // 1. 设置Endpoint、AccessKeyId、AccessKeySecret等信息。 String endpoint = "your-endpoint"; String accessKeyId = "your-accessKeyId"; String accessKeySecret = "your-accessKeySecret"; String bucketName = "your-bucketName"; String objectName = "your-objectName"; String localFilePath = "your-localFilePath"; String newFileName = "your-newFileName"; // 2. 创建OSSClient实例。 ObsClient obsClient = new ObsClient(endpoint, accessKeyId, accessKeySecret); // 3. 下载文件重命名。 obsClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFilePath), newFileName); // 4. 关闭OSSClient。 obsClient.shutdown(); } } ``` 其中,需要替换的参数有: - `your-endpoint`: OSS服务的Endpoint,例如`http://oss-cn-hangzhou.aliyuncs.com`。 - `your-accessKeyId`: 访问OSS的AccessKeyId。 - `your-accessKeySecret`: 访问OSS的AccessKeySecret。 - `your-bucketName`: 存储文件的Bucket名称。 - `your-objectName`: 待下载文件名称。 - `your-localFilePath`: 下载文件后保存到本地的路径。 - `your-newFileName`: 下载重命名文件名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值