将file转变成contenthash
一、将MultipartFile转file
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File fil = fi.getStoreLocation();
二、将file转换成contenthash
String contenthashSource = Utils.encryptSHA(Utils.file2byte(fil));
String firstFileSource = contenthashSource.substring(0, 2); //获取前两位作为存储一级文件夹
String secondFileSource = contenthashSource.substring(2, 4); //获取这两位作为存储二级文件夹 contenthash作为文件名
三、utils实体类
package cn.edu.hbcf.tools; 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.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map; import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; public class Utils {
/**
* 定义加密方式
*/
private final static String KEY_SHA1 = "SHA-1"; /**
* 全局数组
*/
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; /**
* SHA 加密
* @param data 需要加密的字节数组
* @return 加密之后的字节数组
* @throws Exception
*/
public static String encryptSHA(byte[] data) throws Exception {
// 创建具有指定算法名称的信息摘要
// MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
MessageDigest sha = MessageDigest.getInstance(KEY_SHA1);
// 使用指定的字节数组对摘要进行最后更新
sha.update(data);
// 完成摘要计算并返回
return byteArrayToHexString(sha.digest());
}
/**
* 转换字节数组为十六进制字符串
* @param bytes 字节数组
* @return 十六进制字符串
*/
private static String byteArrayToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
sb.append(byteToHexString(bytes[i]));
}
return sb.toString();
}
/**
* 将一个字节转化成十六进制形式的字符串
* @param b 字节数组
* @return 字符串
*/
private static String byteToHexString(byte b) {
int ret = b;
//System.out.println("ret = " + ret);
if (ret < 0) {
ret += 256;
}
int m = ret / 16;
int n = ret % 16;
return hexDigits[m] + hexDigits[n];
}
public static byte[] file2byte(File file)
{
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
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;
}
/**
*
* @方法描述: 将office文件装换成pdf存储
* @param
* @return void
* @author 全冉
* @date 2016-2-3
*/
public static void convert(File input, File output, DocumentFormat outputFormat){
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(input, output, outputFormat);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
}
/**
*
* @方法描述: 将文件复制到一个指定位置
* @param
* @return void
* @author 全冉
* @date 2016-2-3
*/
public static void copy(File fil,File f){
FileChannel in = null;
FileChannel out = null;
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = new FileInputStream(fil);
outStream = new FileOutputStream(f);
in = inStream.getChannel();
out = outStream.getChannel();
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
in.close();
outStream.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
将file转变成contenthash的更多相关文章
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- HTML中上传与读取图片或文件(input file)----在路上(25)
input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...
- logstash file输入,无输出原因与解决办法
1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...
- input[tyle="file"]样式修改及上传文件名显示
默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...
- .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍
1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...
- [笔记]HAproxy reload config file with uninterrupt session
HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...
- VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%
1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...
- input type='file'上传控件假样式
采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- FILE文件流的中fopen、fread、fseek、fclose的使用
FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
随机推荐
- C#对HTML文档的解析
http://www.2cto.com/kf/201312/268777.html http://jingyan.baidu.com/article/7e44095334bb162fc0e2efad. ...
- Hadoop第3周练习--Hadoop2.X编译安装和实验
作业题目 位系统下进行本地编译的安装方式 选2 (1) 能否给web监控界面加上安全机制,怎样实现?抓图过程 (2)模拟namenode崩溃,例如将name目录的内容全部删除,然后通过secondar ...
- jquery选择器(二)-层次选择器
1. 后代选择器 2. 子元素选择器 3. 相邻兄弟元素选择器 4. 一般兄弟元素选择器(同辈元素选择器) 5. 补充(除自身以外的前后同辈选择器)
- Tomcat 服务器性能优化
简介 考虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点.但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝.客户总是期望它们的应用应该有更好的性能.如果你在 ...
- mac os 基本命令
unix 系统命令行 ,仅供参考 目录操作 命令名 功能描述 使用举例 mkdir 创建一个目录 mkdir dirname rmdir 删除一个目录 rmdir dirname ...
- 从输入 URL 到浏览器接收的过程中发生了什么事情?
从输入 URL 到浏览器接收的过程中发生了什么事情? What really happens when you navigate to a URL 上面两篇文章都解读的很好,值得阅读. 接下来在总结一 ...
- TMethod
onclick是TNotifyEvent类型; type TNotifyEvent = procedure(Sender: TObject) of object; 就是说他是一个过 ...
- BZOJ 1019: [SHOI2008]汉诺塔
Description 一个汉诺塔,给出了移动的优先顺序,问从A移到按照规则移到另一个柱子上的最少步数. 规则:小的在大的上面,每次不能移动上一次移动的,选择可行的优先级最高的. Sol DP. 倒着 ...
- ubuntu系统安装PHP扩展
GD库的安装 sudo apt-get install php5-gd sudo apt-get install php5-gd sudo /etc/init.d/apache2 restart CU ...
- NFS和mount常用参数详解
NFS权限参数配置 ro 只读访问 rw 读写访问 sync 所有数据在请求时写入共享 async NFS在写入数据前可以相应请求 secure NFS通过1024以下的安全TCP/IP端口发送 in ...