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----包含了构成抽象窗口工具集( ...
随机推荐
- vue--自定义验证指令
参考文档: https://cn.vuejs.org/v2/guide/custom-directive.html https://www.cnblogs.com/ilovexiaoming/p/68 ...
- 21ic编辑推荐:从单片机开始的程序运行
一直不清楚单片机中程序的执行过程,就是知道一个程序总是从一个main函数开始执行,然后把程序段存放在ROM里面,动态数据存放在RAM里面,而单片机的RAM资源又是及其的稀少,所以要省着用,但是到底怎么 ...
- easyui_1
--- easyui.css包括所有组件的css,
- opencv学习网站
强烈推荐一个老外的网站,pyimagesearch 网址:https://www.pyimagesearch.com/
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- Python面向对象:继承和多态
继承与多态简介: 继承可以把父类的所有功能都直接拿过来,这样就不必重零做起,子类只需要新增自己特有的方法,也可以把父类不适合的方法覆盖重写. 动态语言的鸭子类型特点决定了继承不像静态语言那样是必须的. ...
- 基于express + express-myconnection 的请求处理模板
前提: 开发APP时经常会用到后台提供的接口,作为一个一直搞前端的程序员.如何写后台这事不能真的一窍不通把.所以还是多多少少了解一些后台. 以前尝试过用java 的spring + springMVC ...
- C语言概述
打印摄氏度 /* 1.1 使用int类型进行计算 */ #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, ...
- linux基础(2)-基础命令和基础特性
基础命令 命令历史 命令历史的管理 登陆 shell 时,会读取命令历史文件中记录下的命令: ~/.bash_history . 登陆进 shell 后,新执行的命令只会记录在缓存中,这些命令会在用户 ...
- layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
学习网址:http://layer.layui.com/ 下载地址:http://res.layui.com/download/layer-v2.1.zip 我们提到的基础参数主要指调用方法时用到的配 ...