fileUtil文件的上传下载
package com.beisun.mbp.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.channels.FileChannel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
public class FileUtil {
/**
* 上传文件
* @param filePath 上传文件目录
* @param fileName 保存的文件名称
* @param file 文件流类
* @param request 请求
* @throws IOException IO异常
*/
public static void updoadFile(String filePath,String fileName, MultipartFile file,
HttpServletRequest request) throws IOException{
//如果文件夹不存在,则创建文件夹
File dirPath = new File(filePath);
if(!dirPath.exists()){
dirPath.mkdir();
}
File uploadFile = new File(dirPath +"/"+ fileName);
FileCopyUtils.copy(file.getBytes(), uploadFile);
}
/**
* 复制文件
* @param localFileName 原文件地址和文件名
* @param tmpFileName 目标文件地址和文件名
* @throws IOException
*/
public static void copyFile(String localFileName,String tmpFilePath,String tmpFileName) throws IOException{
File localFile = new File(localFileName);
File tmpFile = new File(tmpFilePath,tmpFileName);
FileCopyUtils.copy(localFile, tmpFile);
}
/**
* 删除文件
* @param fileName 文件地址和文件名
* @throws IOException
*/
public static void deleteFile(String fileName) throws IOException{
File localFile = new File(fileName);
localFile.delete();
}
/**
* 下载文件,是向页面输出流,不返回流
* @param filePath 文件服务器存储目录
* @param downloadName 下载文件保存的文件名
* @param fileName 服务器存储文件名
* @param request
* @param response
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("static-access")
public static void downloadFile(String filePath,String downloadName,String fileName,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
fileName = new java.net.URLDecoder().decode(fileName, "utf-8");
downloadName = new java.net.URLDecoder().decode(downloadName, "utf-8");
String path = filePath+fileName;
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((downloadName).getBytes("GBK"), "iso8859-1"));
try {
//以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取文件的is
* @param filePath 文件服务器存储目录
* @param fileName 服务器存储文件名
* @param request
* @param response
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("static-access")
public static InputStream getFileIS(String filePath,String fileName,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
fileName = new java.net.URLDecoder().decode(fileName, "utf-8");
String path = filePath+fileName;
InputStream fis = null;
try {
//以流的形式下载文件
fis = new BufferedInputStream(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return fis;
}
/**
* 保存文件
* @param datas 文件数据
* @param pathName 文件路径
*/
public static void saveFile(byte[] datas,String pathName){
File file = new File(pathName);
//寤虹珛杈撳嚭瀛楄妭娴�
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
//鐢‵ileOutputStream 鐨剋rite鏂规硶鍐欏叆瀛楄妭鏁扮粍
fos.write(datas);
//涓轰簡鑺傜渷IO娴佺殑寮�攢锛岄渶瑕佸叧闂�
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取文件数据
* @param pathName 文件路径
* @return
*/
public static byte[] getFile(String pathName){
File file = new File(pathName);
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 计算文件大小文件大小
* @param filePath 文件路径例如:E:\\imgData\\afr\\9211496189393485.jpg
* @return 文件大小 Kb
*/
public static long GetFileSize(String filePath){
long fileSize=0l;
FileChannel fc= null;
try {
File f= new File(filePath);
if (f.exists() && f.isFile()){
FileInputStream fis= new FileInputStream(f);
fc= fis.getChannel();
fileSize=fc.size()/1024;
//logger.info(fileSize);
}else{
//logger.info("file doesn't exist or is not a file");
}
} catch (FileNotFoundException e) {
//logger.error(e);
} catch (IOException e) {
//logger.error(e);
} finally {
if (null!=fc){
try{
fc.close();
}catch(IOException e){
//logger.error(e);
}
}
}
return fileSize;
}
/**
* getImage 根据图片的路径获取图片给前台
* @author sunjianbo
* @time 2016年8月25日上午10:41:37
* @param response
* @param request
* @param filePath
* @throws Exception
*/
//@RequestMapping(value = "/getImage.do", method = RequestMethod.GET)
public void getImage(HttpServletResponse response, HttpServletRequest request,String filePath)
throws Exception {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
byte[] data = getFile(filePath);
bos.write(data);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null)
bos.close();
}
}
}
fileUtil文件的上传下载的更多相关文章
- Spring实现文件的上传下载
背景:之前一直做的是数据库的增删改查工作,对于文件的上传下载比较排斥,今天研究了下具体的实现,发现其实是很简单.此处不仅要实现单文件的上传,还要实现多文件的上传. 单文件的下载知道了,多文件的下载呢? ...
- 在Window的IIS中创建FTP的Site并用C#进行文件的上传下载
文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机. 这些文件存储在运行 FTP 服务器软件的服务器计算机上. 然后,远程计算机可以使用 FTP ...
- 创建FTP的Site并用C#进行文件的上传下载
创建FTP的Site并用C#进行文件的上传下载 文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机. 这些文件存储在运行 FTP 服务器软件的服 ...
- linux链接及文件互相上传下载
若排版紊乱可查看我的个人博客原文地址 基本操作 本篇博客主要介绍如何去链接远程的linux主机及如何实现本地与远程主机之间文件的上传下载操作,下面的linux系统是CentOS6.6 链接远程linu ...
- SocketIo+SpringMvc实现文件的上传下载
SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...
- JAVAWEB之文件的上传下载
文件上传下载 文件上传: 本篇文章使用的文件上传的例子使用的都是原生技术,servelt+jdbc+fileupload插件,这也是笔者的习惯,当接触到某些从未接触过的东西时,总是喜欢用最原始的东西将 ...
- SSM框架之中如何进行文件的上传下载
SSM框架的整合请看我之前的博客:http://www.cnblogs.com/1314wamm/p/6834266.html 现在我们先看如何编写文件的上传下载:你先看你的pom.xml中是否有文件 ...
- python使用ftplib模块实现FTP文件的上传下载
python已经默认安装了ftplib模块,用其中的FTP类可以实现FTP文件的上传下载 FTP文件上传下载 # coding:utf8 from ftplib import FTP def uplo ...
- php文件夹上传下载控件分享
用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个php文件夹上传下载的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助. 以下是实例的部分脚本文件 这里我先 ...
随机推荐
- 《网络攻防》Web安全基础实践
20145224陈颢文 <网络攻防>Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御: 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,黑客利用这个bug在数 ...
- float 为什么可以表示很大的整数
1.float型:单精度浮点数在机内占4个字节,用32位二进制描述(注意:计算机中1个字节=8位). 2.浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分. 3.可以算出float型 ...
- Java多线程 - 线程同步
多线程操作同一个对象时,容易引发线程安全问题.为了解决线程安全问题,Java多线程引入了同步监视器. 同步代码块 同步代码块语法格式如下: synchronized(obj){ //此处的代码即为同步 ...
- vue 的小秘密
1.组件可以通过$refs调用其方法. 2.组件上也可用v-model. <input v-model="something"> == 同等 <input v-b ...
- 《JavaScript高级程序设计》第7章 函数表达式
定义函数的方式有两种:函数声明和函数表达式 // 函数声明 function function_name(argument) { // body... } // 函数表达式 var function_ ...
- SPOJ - LCS2
后缀自动机板子题 https://vjudge.net/problem/28017/origin 找多串的最长公共子串 //#pragma comment(linker, "/stack:2 ...
- HDU - 4746预处理莫比乌斯反演
链接 求[1,n] 和 [1,m]中有多少对数的GCD的素因子个数小于等于p 直接暴力做特定超时,所以我们想办法预处理,对于p大于18(1到5e5的最大素数因子个数)的情况,每一对都满足条件,O(1) ...
- java String转Long两种方法区别
第一种:包装类型:Byte,Integer,Short,Long,Boolean,Character,Float,Double等8种 Long.valueOf("String")返 ...
- dga2
0e527eaf_5ec5_4623_9fe9_e459583acd72.com0fmgm1cuu7h1279dghgka0ltg.com0ydlanpuh4e2wl9h6udk6.com10uz8k ...
- SQL正则表达式
(转自:http://blog.csdn.net/xu1314/article/details/10174067) 当我们要进行一些简单的糊涂查询时用百分号(%),通配符(_)就可以了.其中%表达任意 ...