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常用功能的更多相关文章

  1. JAVA基础语法:常用功能符以及循环结构和分支结构(转载)

    3.JAVA基础语法:常用功能符以及循环结构和分支结构 1.常用功能符 注释 ("文字"是被注释的部分) //文字 单行注释 /文字/ 多行注释 算术运算符 + - * / / 整 ...

  2. java常用英文解释

    java常用名词解释: OO: object-oriented ,面向对象 OOP:object-oriented programming,面向对象编程 Author:JCC Object:对象JDK ...

  3. JavaScript 常用功能总结

    小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...

  4. Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库

    Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库1.1. 5种常用的Photoshop滤镜,分别针对照片的曝光.风格色调.黑白照片处理.锐利度.降噪这五大 ...

  5. JAVA常用的XML解析方法

    转并总结自(java xml) JAVA常用的解析xml的方法有四种,分别是DOM,JAX,JDOM,DOM4j xml文件 <?xml version="1.0" enco ...

  6. java基础3.0:Java常用API

    本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...

  7. 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 ...

  8. Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB

    Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...

  9. java 常用的包 默认导入的包

    1.java.lang----包含一些Java语言的核心类,如String.Math.Integer.System和Thread,提供常用功能. 2.java.awt----包含了构成抽象窗口工具集( ...

随机推荐

  1. ++ fatal error C1083: 无法打开预编译头文件:“.\Debug\router.pch”

    一.出现此错误首先检查:stdafx.cpp文件上右键——属性,预编译头选“创建”,其它cpp选“使用”. 二.如果是采用这样的设置,还是有错误,重新生成解决方案,重新调试. 三.实在不行的话,步骤/ ...

  2. 一个不错的工具推荐:JMeter

    在开发中可能会遇到一些场景需要对程序的性能,并发能力等进行度量,就是对一些程序的性能进行度量,生成一些报告等,最近遇到了一个不错的工具 apache JMeter,它是用java的swing开发的,功 ...

  3. C语言位操作--判断两整数是否异号

    判断两整数是否异号: int x, y; //输入比较的两数 bool f = ((x ^ y) < 0); // 返回真,当且仅当x与y异号 说明:当x.y异号,x与y的最高位分别为0和1,取 ...

  4. CentOS 安装Passenger

    gem install passenger 查看路径 passenger-config --root passenger-install-apache2-module ps auxw | grep f ...

  5. WIN7开启wifi热点

    1.首先,先确定自己的笔记本网卡支持“启动承载网络”的功能,使用管理员运行cmd命令.启用管理员运行CMD的方法Windows-所有程序-附件-运行(右键,以管理员身份运行)在弹出的CMD窗口里面敲击 ...

  6. 关于nagios系统下使用shell脚本自定义监控插件的编写

    在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控.检测功能都是通过各种插件来完成的. 启动Nagios ...

  7. 【BZOJ4355】Play with sequence 线段树

    [BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...

  8. Xcode快速排错

    EXTENDS:http://blog.csdn.net/guo_hongjun1611/article/details/8063009 1,模拟器运行完全没问题,真机运行失败. 有时候我们在模拟器上 ...

  9. 【C#】浏览器源代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 解析xml文件的几种技术与Dom4j与sax之间的对比

    一.解析xml文件的几种技术:dom4j.sax.jaxb.jdom.dom 1.dom4j dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常优秀的 ...