文件的上传和下载

mac2025-03-20  15

application.yml

upload: localtion: windows: /system/uploadFile/ linux: /var/local/system/uploadFile/ mac: /Users/primrose/Desktop/uploadFile/ maxFileSize: 10240KB maxRequestSize: 102400KB

FileController

import com.alibaba.fastjson.JSON; import com.youruan.common.utils.DateUtil; import com.youruan.framework.config.UploadProperties; import com.youruan.framework.config.model.FileUploadResponse; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.ResourceLoader; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Pattern; @Api(value = "文件管理", tags = {"文件的上传"}) @RestController public class FileController { private Logger log = LoggerFactory.getLogger(FileController.class); @Resource private ResourceLoader resourceLoader; @Resource private UploadProperties uploadProperties; private String reg = "(mp4|flv|avi|rm|rmvb|wmv)"; @PostMapping("/upload") @ApiOperation(value = "保存", notes = "保存一条数据信息") @ApiImplicitParam(paramType = "header", dataType = "String", name = "Authorization", value = "'Bearer '", required = true) public List<FileUploadResponse> upload(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) { List<FileUploadResponse> list = new ArrayList<FileUploadResponse>(); // 获取文件存放路径 String path = uploadProperties.getBasePath() + DateUtil.format(new Date(), "yyyyMMdd") + "/"; // 判断文件夹是否存在,不存在则创建路径 File targetFile = new File(path); if (!targetFile.exists()) { targetFile.mkdirs(); } for (MultipartFile file : files) { FileUploadResponse rs = new FileUploadResponse(); String contentType = file.getContentType(); String originalFilename = file.getOriginalFilename(); String fileName = String.valueOf(System.currentTimeMillis()) + originalFilename.substring(originalFilename.lastIndexOf(".")); try { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(path + fileName))); out.write(file.getBytes()); out.flush(); out.close(); rs.setContentType(contentType); rs.setFileName(fileName); if (path.contains("/var/local")) { String str = "/var/local"; int index = path.indexOf("/var/local") + str.length(); System.out.println(index); path = path.substring(index, path.length()); } rs.setUrl(path + fileName); rs.setType("success"); rs.setMsg("文件上传成功"); log.info("文件上传成功:" + JSON.toJSONString(rs)); } catch (Exception e) { rs.setType("fail"); rs.setMsg("文件上传失败"); log.error("上传文件失败:" + e); } list.add(rs); } return list; } /** * @param date * @param filename * @return * @Description: 显示图片for mac */ @GetMapping(value = "/Users/primrose/Desktop/uploadFile/{date}/{filename:.+}") public ResponseEntity<?> getFileForMac(@PathVariable String date, @PathVariable String filename,HttpServletRequest request, HttpServletResponse response) throws IOException { Pattern p = Pattern.compile(reg); boolean boo = p.matcher(filename).find(); if(boo){ String path = Paths.get(uploadProperties.getBasePath() + File.separator + date, filename).toString(); File file = new File(path); sendVideo(request, response, file, filename); }else{ try { return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(uploadProperties.getBasePath() + File.separator + date, filename).toString())); } catch (Exception e) { return ResponseEntity.notFound().build(); } } return null; } /** * @param fileName * @return ResponseEntity<?> 返回类型 * @Title: download * @Description: 下载 */ @RequestMapping(value = "/download", method = RequestMethod.GET) public ResponseEntity<InputStreamResource> downloadFile(@RequestParam String fileName) throws IOException { String classpath = "D:\\system"; String filePath = classpath + fileName; FileSystemResource file = new FileSystemResource(filePath); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename())); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); return ResponseEntity .ok() .headers(headers) .contentLength(file.contentLength()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(file.getInputStream())); } /** * @param date * @param filename * @return * @Description: 显示文件 */ @GetMapping(value = "/system/uploadFile/{date}/{filename:.+}") public ResponseEntity<?> getFile(@PathVariable String date, @PathVariable String filename,HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException { Pattern p = Pattern.compile(reg); boolean boo = p.matcher(filename).find(); if(boo){ String path = Paths.get(uploadProperties.getBasePath() + File.separator + date, filename).toString(); File file = new File(path); sendVideo(request, response, file, filename); }else{ try { return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(uploadProperties.getBasePath() + File.separator + date, filename).toString())); } catch (Exception e) { return ResponseEntity.notFound().build(); } } return null; } private void sendVideo(HttpServletRequest request, HttpServletResponse response, File file, String fileName) throws FileNotFoundException, IOException { RandomAccessFile randomFile = new RandomAccessFile(file, "r");//只读模式 long contentLength = randomFile.length(); String range = request.getHeader("Range"); int start = 0, end = 0; if(range != null && range.startsWith("bytes=")){ String[] values = range.split("=")[1].split("-"); start = Integer.parseInt(values[0]); if(values.length > 1){ end = Integer.parseInt(values[1]); } } int requestSize = 0; if(end != 0 && end > start){ requestSize = end - start + 1; } else { requestSize = Integer.MAX_VALUE; } byte[] buffer = new byte[4096]; response.setContentType("video/mp4"); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("ETag", fileName); response.setHeader("Last-Modified", new Date().toString()); //第一次请求只返回content length来让客户端请求多次实际数据 if(range == null){ response.setHeader("Content-length", contentLength + ""); }else{ //以后的多次以断点续传的方式来返回视频数据 response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);//206 long requestStart = 0, requestEnd = 0; String[] ranges = range.split("="); if(ranges.length > 1){ String[] rangeDatas = ranges[1].split("-"); requestStart = Integer.parseInt(rangeDatas[0]); if(rangeDatas.length > 1){ requestEnd = Integer.parseInt(rangeDatas[1]); } } long length = 0; if(requestEnd > 0){ length = requestEnd - requestStart + 1; response.setHeader("Content-length", "" + length); response.setHeader("Content-Range", "bytes " + requestStart + "-" + requestEnd + "/" + contentLength); }else{ length = contentLength - requestStart; response.setHeader("Content-length", "" + length); response.setHeader("Content-Range", "bytes "+ requestStart + "-" + (contentLength - 1) + "/" + contentLength); } } ServletOutputStream out = response.getOutputStream(); int needSize = requestSize; randomFile.seek(start); while(needSize > 0){ int len = randomFile.read(buffer); if(needSize < buffer.length){ out.write(buffer, 0, needSize); } else { out.write(buffer, 0, len); if(len < buffer.length){ break; } } needSize -= buffer.length; } randomFile.close(); out.close(); } }

UploadProperties

package com.youruan.examine.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; @Component @ConfigurationProperties("upload") public class UploadProperties { /** * 获取存放位置 */ private Map<String, String> localtion; /** * 单个文件大小 */ private String maxFileSize; /** * 单次上传总文件大小 */ private String maxRequestSize; public Map<String, String> getLocaltion() { return localtion; } public void setLocaltion(Map<String, String> localtion) { this.localtion = localtion; } public String getMaxFileSize() { return maxFileSize; } public void setMaxFileSize(String maxFileSize) { this.maxFileSize = maxFileSize; } public String getMaxRequestSize() { return maxRequestSize; } public void setMaxRequestSize(String maxRequestSize) { this.maxRequestSize = maxRequestSize; } public String getBasePath() { String location; String os = System.getProperty("os.name"); String windows = "win"; if(os.toLowerCase().startsWith(windows)) { location = this.getLocaltion().get("windows"); } else if(os.toLowerCase().startsWith("mac")) { location = this.getLocaltion().get("mac"); }else{ location = this.getLocaltion().get("linux"); } return location; } @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("20MB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } }

FileUploadResponse

package com.youruan.examine.config.model; public class FileUploadResponse { private String contentType; private String fileName; private String url; private String type; private String msg; public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }

DateUtil

package com.youruan.examine.util; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DateFormat; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.*; /** * 说明: 日期时间工具类 */ public class DateUtil { private static Logger log = LoggerFactory.getLogger(DateUtil.class); public static final String PATTERN_YM = "yyyy-MM"; // pattern_ym public static final int PATTERN_YM_LENGTH = 7; public static final String PATTERN_YMD = "yyyy-MM-dd"; // pattern_ymd public static final int PATTERN_YMD_LENGTH = 10; public static final String PATTERN_YMD_HM = "yyyy-MM-dd HH:mm"; // pattern_ymd hm public static final int PATTERN_YMD_HM_LENGTH = 16; public static final String PATTERN_YMD_HMS = "yyyy-MM-dd HH:mm:ss"; // pattern_ymd time public static final int PATTERN_YMD_HMS_LENGTH = 19; public static final String PATTERN_YMD_HMS_S = "yyyy-MM-dd HH:mm:ss:SSS"; // pattern_ymd timeMillisecond public static final int PATTERN_YMD_HMS_S_LENGTH = 23; public static final String PATTERN_YMDHMS = "yyyyMMddHHmmss"; /** * 返回年份 * * @param date 日期 * * @return 返回年份 */ public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /** * 返回 年 月 日 */ public static Map<String, Object> getDateMsg(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); Map<String, Object> map = new HashMap<>(); map.put("year", calendar.get(Calendar.YEAR)); map.put("month", calendar.get(Calendar.MONTH)); map.put("day", calendar.get(Calendar.DAY_OF_MONTH)); return map; } /** * 格式化 * * @param date * @param pattern * * @return */ public static String format(Date date, String pattern) { DateFormat format = new SimpleDateFormat(pattern); return format.format(date); } /** * 格式化 * * @param date * @param pattern * @param timeZone * * @return */ public static String format(Date date, String pattern, TimeZone timeZone) { DateFormat format = new SimpleDateFormat(pattern); format.setTimeZone(timeZone); return format.format(date); } /** * 格式化 * * @param date * @param parsePattern * @param returnPattern * * @return */ public static String format(String date, String parsePattern, String returnPattern) { return format(parse(date, parsePattern), returnPattern); } /** * 格式化 * * @param date * @param parsePattern * @param returnPattern * @param timeZone * * @return */ public static String format(String date, String parsePattern, String returnPattern, TimeZone timeZone) { return format(parse(date, parsePattern), returnPattern, timeZone); } /** * 时间戳转时间 */ public static Date timeToDate(Date date, String format) { if (date == null) { return null; } if (StringUtils.isBlank(format)) { format = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(format); String str = sdf.format(date.getTime()); return sdf.parse(str, new ParsePosition(1)); } /** * 解析 * * @param date * @param pattern * * @return */ public static Date parse(String date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern); try { return format.parse(date); } catch (ParseException e) { log.error("ToolDateTime.parse异常:date值" + date + ",pattern值" + pattern); return null; } } /** * 解析 * * @param date * * @return */ public static Date parse(String date) { return parse(date, PATTERN_YMD_HMS); } /** * 解析 * * @param date * * @return */ public static Date parseYearAndDay(String date) { return parse(date, PATTERN_YMD); } /** * 两个日期的时间差,返回"X天X小时X分X秒" * * @param start * @param end * * @return */ public static String getBetween(Date start, Date end) { long between = (end.getTime() - start.getTime()) / 1000;// 除以1000是为了转换成秒 long day = between / (24 * 3600); long hour = between % (24 * 3600) / 3600; long minute = between % 3600 / 60; long second = between % 60 / 60; String sb = String.valueOf(day) + "天" + hour + "小时" + minute + "分";//second + "秒"; return sb; } /** * 返回两个日期之间隔了多少秒 * * @param start * @param end * * @return */ public static int getDateSecondSpace(Date start, Date end) { return (int) ((end.getTime() - start.getTime()) / (1000)); } /** * 返回两个日期之间隔了多少分钟 * * @param start * @param end * * @return */ public static int getDateMinuteSpace(Date start, Date end) { return (int) ((end.getTime() - start.getTime()) / (60 * 1000)); } /** * 返回两个日期之间隔了多少小时 * * @param start * @param end * * @return */ public static int getDateHourSpace(Date start, Date end) { return (int) ((end.getTime() - start.getTime()) / (60 * 60 * 1000)); } /** * 返回两个日期之间隔了多少天 * * @param start * @param end * * @return */ public static int getDateDaySpace(Date start, Date end) { return (int) ((end.getTime() - start.getTime()) / (60 * 60 * 24 * 1000)); } public static boolean isSameDate(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR); boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH); boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH); return isSameDate; } //根据日期取得星期几 public static String getWeek(Date date) { String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY; if (weekIndex < 0) { weekIndex = 0; } return weeks[weekIndex]; } /** * 返回两个日期之间隔了多少天,包含开始、结束时间 * * @param start * @param end * * @return */ public static List<String> getDaySpaceDate(Date start, Date end) { Calendar fromCalendar = Calendar.getInstance(); fromCalendar.setTime(start); fromCalendar.set(Calendar.HOUR_OF_DAY, 0); fromCalendar.set(Calendar.MINUTE, 0); fromCalendar.set(Calendar.SECOND, 0); fromCalendar.set(Calendar.MILLISECOND, 0); Calendar toCalendar = Calendar.getInstance(); toCalendar.setTime(end); toCalendar.set(Calendar.HOUR_OF_DAY, 0); toCalendar.set(Calendar.MINUTE, 0); toCalendar.set(Calendar.SECOND, 0); toCalendar.set(Calendar.MILLISECOND, 0); List<String> dateList = new LinkedList<>(); long dayCount = (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24); if (dayCount < 0) { return dateList; } dateList.add(format(fromCalendar.getTime(), PATTERN_YMD)); for (int i = 0; i < dayCount; i++) { fromCalendar.add(Calendar.DATE, 1);// 增加一天 dateList.add(format(fromCalendar.getTime(), PATTERN_YMD)); } return dateList; } /** * 时间按年相加减 * 正值相加 * 负值相减 */ public static Date calcDateByYear(Date date, int year) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.YEAR, year); return calendar.getTime(); } /** * 时间按年天相加减 * 正值相加 * 负值相减 */ public static Date calcDateByDay(Date date, int day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, day); return calendar.getTime(); } /** * 时间按小时相加减 * 正值相加 * 负值相减 */ public static Date calcDateByHour(Date date, int hour) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR, hour); return calendar.getTime(); } /** * 时间按分相加减 * 正值相加 * 负值相减 */ public static Date calcDateByMinute(Date date, int minutes) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, minutes); return calendar.getTime(); } /** * 判断时间是否在时间段内 */ public static boolean compareDate(Date start, Date end, Date date) { return date.after(start) && date.before(end); } /** * 获取开始时间 * * @param start * @param end * * @return */ public static Date startDateByDay(Date start, int end) { Calendar calendar = Calendar.getInstance(); calendar.setTime(start); calendar.add(Calendar.DATE, end);// 明天1,昨天-1 calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 获取结束时间 * * @param start * * @return */ public static Date endDateByDay(Date start) { Calendar calendar = Calendar.getInstance(); calendar.setTime(start); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } /** * 获取开始时间 * * @param start * @param end * * @return */ public static Date startDateByHour(Date start, int end) { Calendar calendar = Calendar.getInstance(); calendar.setTime(start); calendar.set(Calendar.MINUTE, end); return calendar.getTime(); } /** * 获取结束时间 * * @param end * * @return */ public static Date endDateByHour(Date end) { Calendar calendar = Calendar.getInstance(); calendar.setTime(end); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } public static void main(String[] args) { System.out.println(DateUtil.format(DateUtil.calcDateByYear(new Date(), 1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.calcDateByDay(new Date(), 1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.calcDateByHour(new Date(), 1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.calcDateByYear(new Date(), -1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.calcDateByDay(new Date(), -1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.calcDateByHour(new Date(), -1), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.startDateByDay(new Date(), 0), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.format(DateUtil.endDateByDay(new Date()), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.getDateDaySpace(DateUtil.parse("2015-01-01", "yyyy-MM-dd"), new Date())); System.out.println(DateUtil.getDaySpaceDate(DateUtil.parse("2016-04-01 8:00:00", "yyyy-MM-dd HH:mm:ss"), new Date())); System.out.println(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")); System.out.println(DateUtil.parse("2015-11-21 12:34:56", "yyyy-MM-dd HH:mm:ss")); } }

Constant

public class Constant { public final static String ROOT_PATH = "D:\\system"; }

上传图片实现

图片显示

http://localhost:8090/system/uploadFile/20191101/1572575996741.jpg 直接打开即可

最新回复(0)