java常用功能
1.复制文件
private void fileChannelCopy(File source, File target) throws IOException {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null; try {
fi = new FileInputStream(source);
fo = new FileOutputStream(target);
in = fi.getChannel();
out = fo.getChannel();
in.transferTo(, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
LOGGER.error("File copy failed.", e);
}
}
}
2. restApi 显示图片
@GET
@Path("/")
@Produces("image/*")public StreamingOutput getImage(final @QueryParam("path") String path) throws IOException { LOGGER.debug("source:" + path); return new StreamingOutput() { @Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
try (
InputStream in = storage.getDocument(path)) {
output.write(IOUtils.readBytesFromStream(in));
} catch (final FileNotFoundException ex) {
LOGGER.error("file is not founded.", ex);
throw new NotFoundException();
} }
};
}
public class StorageUtil {
private final File folder = new File("/macc/img"); public StorageUtil() throws IOException {
if (!folder.exists() && !folder.mkdirs()) {
throw new IOException("Unable to initialize FS storage:" + folder.getAbsolutePath());
} if (!folder.isDirectory() || !folder.canWrite() || !folder.canRead()) {
throw new IOException("Unable to access FS storage:" + folder.getAbsolutePath());
}
} public void addDocument(final String name, final byte[] content) throws IOException {
try (InputStream in = new ByteArrayInputStream(content)) {
addDocument(name, in);
}
} public void addDocument(final String name, final InputStream in) throws IOException {
final File f = new File(folder, name); if (f.exists() && !f.delete()) {
throw new IOException("Unable to delete FS file:" + f.getAbsolutePath());
} try (OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) {
out.write(IOUtils.readBytesFromStream(in)); f.deleteOnExit();
}
} public InputStream getDocument(final String name) throws IOException {
String path = folder + "/" + name;
final File f = new File(path); if (!f.exists() || !f.isFile()) {
throw new FileNotFoundException("Unable to access FS file:" + f.getAbsolutePath());
} return new FileInputStream(f);
} public void deleteAll() throws IOException {
for (final File f: folder.listFiles()) {
if (!f.delete()) {
throw new IOException("Unable to delete FS file:" + f.getAbsolutePath());
}
}
}
}
3.上传图片-表单模式
@POST
@Consumes("multipart/form-data")
@Path("/upload")
public Response uploadFileByForm(@Context
HttpServletRequest imgRequest,
@Multipart(value="name",type="text/plain")String name,
@Multipart(value="file")Attachment image) { DataHandler dh = image.getDataHandler(); try {
InputStream ins = dh.getInputStream();
writeToFile(ins, "/macc/img/" + dh.getName());
} catch (Exception e) {
LOGGER.error("upload failed.", e);
} return Response.ok().entity("ok").build();
}
4.图片上传-二进制流模式
@POST
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@ModuleSecurityAuth(moduleId=ModuleId.PLAN ,privilegeOperation=PrivilegeOperation.WRITE)
@Path("/uploadstream")
public UploadFileRsp uploadFile(@Context
HttpServletRequest imgRequest) {
UploadFileRsp response =new UploadFileRsp();
try {
InputStream ins = imgRequest.getInputStream();
Integer groupId = Integer.parseInt(imgRequest.getParameter("group_id"));
String name = imgRequest.getParameter("name");
BufferedImage bi =ImageIO.read(ins);
int height = bi.getHeight();
int width = bi.getWidth();
String storeName = getStoreName(groupId);
String path = tenantId() + "/" + storeName;
File out = new File(PLAN_PICTURE_PATH + path); //没有文件夹则创建
if(!out.getParentFile().exists())
out.getParentFile().mkdirs(); ImageIO.write(bi, getSuffix(name), out);
pictureService.bindPicture2Floor(path, name, tenantId(), userId(), groupId, width, height);
response.setHeight(height);
response.setPath(path);
response.setWidth(width);
} catch (Exception e) {
LOGGER.error("upload failed.", e);
response.buildCode(RestCode.PICTURE_UPLOAD_ERROR);
} return response;
}
java常用功能的更多相关文章
- JAVA基础语法:常用功能符以及循环结构和分支结构(转载)
3.JAVA基础语法:常用功能符以及循环结构和分支结构 1.常用功能符 注释 ("文字"是被注释的部分) //文字 单行注释 /文字/ 多行注释 算术运算符 + - * / / 整 ...
- java常用英文解释
java常用名词解释: OO: object-oriented ,面向对象 OOP:object-oriented programming,面向对象编程 Author:JCC Object:对象JDK ...
- JavaScript 常用功能总结
小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...
- Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库
Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库1.1. 5种常用的Photoshop滤镜,分别针对照片的曝光.风格色调.黑白照片处理.锐利度.降噪这五大 ...
- JAVA常用的XML解析方法
转并总结自(java xml) JAVA常用的解析xml的方法有四种,分别是DOM,JAX,JDOM,DOM4j xml文件 <?xml version="1.0" enco ...
- java基础3.0:Java常用API
本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...
- Java常用jar包用途
Java常用jar包用途: USAGE INDEX JAR NAME USAGE 1 ASM asm-2.2.3.jar ASM字节码库 2 ASM asm-commons-2.2.3.jar ASM ...
- Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB
Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...
- java 常用的包 默认导入的包
1.java.lang----包含一些Java语言的核心类,如String.Math.Integer.System和Thread,提供常用功能. 2.java.awt----包含了构成抽象窗口工具集( ...
随机推荐
- testmath
$\Huge ans = \frac{ \sum\limits_{i=1}^{m}{ (x_i - \bar{x})^2 } }{m} \cdot m^2 \\$
- 算法学习之快速排序的C语言实现
近几天在学习简单算法,今天看了一个快速排序和堆排序,堆排序还没搞懂,还是先把快速排序搞清楚吧 教程网上一艘一大堆,这里选择一个讲的比较通俗的的一个吧: http://blog.csdn.net/mor ...
- Elasticsearch 自定义映射
尽管在很多情况下基本域数据类型 已经够用,但你经常需要为单独域自定义映射 ,特别是字符串域.自定义映射允许你执行下面的操作: 全文字符串域和精确值字符串域的区别 使用特定语言分析器 优化域以适应部分匹 ...
- Java 8新增的Lambda表达式
一. 表达式入门 Lambda表达式支持将代码块作为方法参数,lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数式接口)的实例,相当于一个匿名的方法. 1.1 La ...
- Mongodb之使用rpm包安装配置启动
下载rpm包 wget https://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/RPMS/mongod ...
- HDU 4848 - Wow! Such Conquering!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descriptio ...
- Oracle安装部署之linux OS install oracle database安装脚本
#!/bin/bash#Purpose:Create and config oracle install.#Usage:Log on as the superuser('root') #1.creat ...
- lmathlib文件
lua的math库是通过lua扩展而来,主要对系统math库进行了一次封装,以供lua使用,一般可以从lmathlib文件开始阅读源码. //绝对值 static int math_abs (lua_ ...
- swiper跳转制定页面
haha(){ var that=this; that.$refs.mySwiper.swiper.slideTo(1, 1000, false); } //以上代码是 获取ref值为myswipe ...
- 一致性 hash 算法(转)
add by zhj:介绍了什么是一致性hash,以及实现一致性hash的一种算法. 原文:http://my.oschina.net/u/195065/blog/193614 目录[-] 一致性 h ...