java文件上传获取文件大小,文件时长 |
您所在的位置:网站首页 › java上传文件格式 › java文件上传获取文件大小,文件时长 |
在java中获取上传文件的时长方法多种,在此我用其中的一种,不过需要引入一个jave.jar,此jar在maven仓库总下载不到,我保存到自己的网盘中了,链接: https://pan.baidu.com/s/1eZrV5uHCVS5M7CPpVIVKJQ 提取码: jzfe 。 获取文件大小用java自带的方法就行 在此用的框架为springMVC 文件的上传 package com.qp.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.math.BigDecimal; import java.net.URLEncoder; import java.text.DecimalFormat; import java.util.Date; import java.util.HashMap; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RequestMapping("/uploads") @RestController public class UploadController { private HashMap map = new HashMap(); //文件上传 @RequestMapping("fileUpload") public HashMap fileUpload(MultipartFile audio,HttpServletRequest request){ try{ //获取文件名 String filename = audio.getOriginalFilename(); //根据相对路径获取绝对路径 String realPath = request.getSession().getServletContext().getRealPath("/audios"); //判断文件夹是否存在 File file = new File(realPath); if(!file.exists()){ file.mkdir(); } String sname = new Date().getTime()+"-"+filename; //获取音频大小 zijie long size = audio.getSize(); System.out.println("文件大小 :"+size); //=========1======== String sizes = size/1024/1024+"MB"; System.out.println("文件大小 :"+sizes); //=========2======== DecimalFormat format = new DecimalFormat("0.00"); String str = String.valueOf(size); Double dd = Double.valueOf(str)/1024/1024; String sizess = format.format(dd)+"MB"; System.out.println("文件大小 :"+sizess); //=========3======== DecimalFormat df = new DecimalFormat("0.00"); Double a = Double.valueOf(String.valueOf(size))/1024/1024; if(a>1024){ a= a/1024; System.out.println("文件大小为: "+df.format(a)+"GB"); }else System.out.println("文件大小为: "+df.format(a)+"MB"); //=========4======== BigDecimal sizez = new BigDecimal(audio.getSize()); BigDecimal mod = new BigDecimal(1024); //除两个1024,保留两位小数,进行四舍五入 BigDecimal setScale = sizez.divide(mod).divide(mod).setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("文件大小为: "+setScale+"MB"); //文件上传 audio.transferTo(new File(realPath,sname)); //获取文件时长 Encoder encoder = new Encoder(); long length = encoder.getInfo(new File(realPath,sname)).getDuration(); //输出文件时长 System.out.println("==length=="+length+"毫秒"); System.out.println("==length/1000=="+length/1000+"秒"); System.out.println("==length=="+length/1000/60+"分"+length/1000%60+"秒"); map.put("success", 200); map.put("message", "上传成功"); } catch (Exception e) { map.put("success", 400); map.put("message", "上传失败"); } return map; } } 文件的下载 //下载音频 @RequestMapping("downloadAudio") public void downloadAudio(String fileName,HttpServletRequest request,HttpServletResponse response) throws Exception{ System.out.println("==========fileName="+fileName); //根据用户名去服务器中的指定位置获取文件 String realPath = request.getSession().getServletContext().getRealPath("/audios"); //读取文件输入流 FileInputStream fis = new FileInputStream(new File(realPath, fileName)); //设置响应的方式 响应头 //attachment;以附件形式下载 inline 在线打开 response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8")); //获取响应流 ServletOutputStream os = response.getOutputStream(); //下载文件 IOUtils.copy(fis,os); //关闭资源 IOUtils.closeQuietly(fis); IOUtils.closeQuietly(os); } 下载PDF文件 @RequestMapping(value = "/personApply/downMaterial", produces = {"application/json;charset=GBK"},method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody public String downMaterial(HttpServletRequest request, HttpServletResponse response) throws Exception { String dirName = request.getSession().getServletContext().getRealPath("/static/pdf"); File file = new File(dirName,"material.pdf"); response.setContentType("application/pdf"); response.addHeader("Content-Disposition","attachment; filename=" + new String("material.pdf".getBytes("GBK"),"ISO8859-1")); FileInputStream fileInputStream = new FileInputStream(file); byte[] by = new byte[fileInputStream.available()]; fileInputStream.read(by); OutputStream outputStream = response.getOutputStream(); outputStream.write(by); fileInputStream.close(); outputStream.close(); return null; }
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |