将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,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
随机推荐
- 大熊君大话NodeJS之------Buffer模块
一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...
- 关于R中的mode()和class()的区别
本文原创,转载请注明出处,本人Q1273314690(交流学习) 说明:本文曾经在15年11月在CSDN发过,但是由于CSDN不支持为知笔记的发布为博客的API功能,所以,自今天起,转移到博客园(幸好 ...
- 透过统计力学,模拟软物质——EPJE专访2016年玻尔兹曼奖得主Daan Frenkel
原文来源:Eur. Phys. J. E (2016) 39: 68 2016年玻尔兹曼奖得主Daan Frenkel接受欧洲物理学报E专访,畅谈统计物理在交叉科学研究中的前所未有的重要性. 统计物理 ...
- oracle中session的查询与删除
1. 查询连接的session select sid,serial#,username,program,machine,status from v$session 查询的结果如下,可以根据机器和登录的 ...
- Javascript高级程序设计——基本概念(二)
相等操作符: 相等==:这个操作符会先转换操作数,强制类型转换,然后再比较他们的相等性. null == undefined //true NaN == NaN //false"5" ...
- IE6对png图片的处理
在学习phpcms系统搜索模块的时候,发现下面这段代码: <!--[if IE 6]> <script type="text/javascript" src=&q ...
- css position:absolute 如何居中对齐
写死宽度,就好弄了 position: absolute;left: 50%;width: 980px;margin-left: -490px; text-algin:center
- angularjs入门基础一
app.controller('firstController',function($scope,$rootScope){ $scope.name='张三'; $rootScope.age='30'; ...
- 不安装Oracle客户端使用PLSQL
要连接Oracle往往需要安装上百兆的ORACLE客户端,显得十分麻烦.先就介绍如何通过使用精简客户端,且不需要安装的客户端,配合PLSQL连接oracle数据库. 其实这些操作都很简单,写在这里 ...
- hiho #1326 : 有序01字符串
#1326 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改 ...