图片下载

下载按钮

<button type=\"button\" class=\"btn btn-success\" οnclick=\"location.href='<%=request.getContextPath() %>/bookController/download.do?bookno=" + data + "'\"><i class='glyphicon glyphicon-cog'></i>下载</button>\n" +

创建 UploadFileUtil 工具类

package org.jq.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

public class UploadFileUtil {

	/**
	 * bootstrap上传插件
	 * @param fileObj
	 * @param request
	 * @return
	 */
	public static Map<String,Object> uploadFileInput(MultipartFile fileObj, HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		//调用上传方法
		String fileName = upload(fileObj,request);
		
		resultMap.put("success", true);
		resultMap.put("filePath", "/images/"+fileName);
		return resultMap;
	}
	/**
	 * 文件上传
	 * 
	 * @param fileObj
	 * @param request
	 * @return
	 */
	public static String upload(MultipartFile fileObj, HttpServletRequest request) {

		if (fileObj == null || fileObj.getSize() == 0)// 假如文件上传配置不正确,可能造成img对象为空,
		{
			System.out.println("MultipartFile img  对象为空,请检查文件上传配置");
			return null;
		}

		// --------------------优化图片名称,让名字不会重复--------------------------------------

		// 文件名称不能重复
		// 3dc9043e-b2af-4b43-ab53-d95e839cfe96
		String imgNewName = UUID.randomUUID().toString(); 使用uuid去生成图片名称

		// 截取原始图片名字的后缀名
		// 如:2.jpg
		String imgOldName = fileObj.getOriginalFilename();
		// 截取后缀名
		imgOldName = imgOldName.substring(imgOldName.lastIndexOf("."));

		// 假如你需要用户,只能上传图片.jpg .png .gif if else判断就行

		// 这个名字,就变成了 uuid + 后缀名 3dc9043e-b2af-4b43-ab53-d95e839cfe96.jpg
		imgNewName = imgNewName + imgOldName;

		// --------------------优化图片名称,让名字不会重复--------------------------------------
		// getServletContext获取servlet上下文
		// getRealPath 获取项目在电脑硬盘上面的路径, / 代表根目录
		// D:\huangpeng\apache-tomcat-8.5.47\webapps\day12_upload\
		String path = request.getServletContext().getRealPath("/images/");
		
		File dir = new File(path);
		if(!dir.exists())//文件夹是否存在,如果不存在,则需要创建文件夹
		{
			dir.mkdirs();
		}
		// --------------------------动态获取项目路径---------------------
		// 需要使用request对象,去获取项目路径

		// --------------------------动态获取项目路径---------------------

		// 创建图片文件对象
		// D:\huangpeng\apache-tomcat-8.5.47\webapps\day12_upload\images\3dc9043e-b2af-4b43-ab53-d95e839cfe96.jpg
		File file = new File(path  + imgNewName);

		// 使用transferTo 方法,保存文件
		try {
			fileObj.transferTo(file);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imgNewName;
	}

	/**
	 * 文件下载
	 */
	public static void download(String imgName, HttpServletRequest request, HttpServletResponse response) {
		
		if(imgName == null || "".equals(imgName))//如果文件名称为空,则直接返回空
		{
			System.out.println("文件名称为空,请断点调试,查看");
			return ;
		}
		
		// D:\huangpeng\apache-tomcat-8.5.47\webapps\day12_upload\images\xxxxxx.jpg

		// 获取项目的绝对路径
		String path = request.getServletContext().getRealPath("/") + imgName;

		// 获取图片在电脑硬盘上的,具体路径
		File file = new File(path);

		// 使用java提供的Files对象,把文件显示给jsp页面,让页面下载
		// 第一个参数,图片的具体路径
		// 第二个参数,使用response 的输出流

		// 需要注意的是,使用输出流后,浏览器并不知道,我们是提供的一个文件下载.
		// 所以,我们需要告诉浏览器,我们现在这个请求,是下载文件
		// 使用response方法,告诉浏览器,这里是一个文件,需要进行下载,并不是直接展示
		response.setContentType("application/x-msdownload");

		// 告诉浏览器返回的文件的名称
		response.setHeader("Content-Disposition", "attachment;filename=" + imgName);

		try {
			Files.copy(file.toPath(), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

控制层代码

	//下载
	@RequestMapping("download")
	public  void download(Integer medicineId, HttpServletRequest request, HttpServletResponse response) {
		Medicine m = medicineService.queryMedicineById(medicineId);
		UploadFileUtil.download(m.getMedicineimg(), request, response);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jq1223

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值