java文件上传获取文件大小,文件时长

您所在的位置:网站首页 java上传文件格式 java文件上传获取文件大小,文件时长

java文件上传获取文件大小,文件时长

2024-07-13 14:12:33| 来源: 网络整理| 查看: 265

在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; }

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭