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. vue 项目要使用的库

    1.Stylus是一个CSS预处理器. npm install stylus --save-dev npm install stylus-loader --save-dev 使用 <style ...

  2. 改变presentModalView大小

    rc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; rc.modalPresentationStyle = UIModalP ...

  3. Laya 位图字体制作(失败...)

    参考: 官网教程-位图字体的制作与使用 一.下载字体并安装字体 从站长字体下载了液晶数字字体,将TTF文件拖入C盘windows/Font文件夹,则字体会自动安装 二.下载字体制作工具 Bitmap ...

  4. 【BZOJ1294】[SCOI2009]围豆豆Bean 射线法+状压DP+SPFA

    [BZOJ1294][SCOI2009]围豆豆Bean Description Input 第一行两个整数N和M,为矩阵的边长. 第二行一个整数D,为豆子的总个数. 第三行包含D个整数V1到VD,分别 ...

  5. Function学习

    ---恢复内容开始--- Function的定义: 1.函数声明 function sum1(){ alert("sum1") } 2.函数表达式 var sum2 = funct ...

  6. ggplot2绘制概率密度图

    以下绘图以Weibull分布(韦伯分布.威布尔分布)为例 关于Weibull分布(韦伯分布.威布尔分布),请参考本人博客http://www.cnblogs.com/wwxbi/p/6141501.h ...

  7. iOS ViewControllers 瘦身

    https://objccn.io/issue-1-1/ https://juejin.im/user/57ddfba4128fe10064cbb93a 把 Data Source 和其他 Proto ...

  8. Codeforces 278B Books

    好久没有写二分了 题意:有n本书 每本书有一个阅读时间ai 要在t时间内读最多的书 读书的顺序是连续的,如果无法读完一本书就不能开始 最开始觉得会是个dp,但是动规方程写不出来.想想会不会是二分呢,也 ...

  9. NET技术公众号已上线

    各位兄弟姐妹,本人构建技术微信号已正式上线,后续的技术分享主要以微信公众号为主,博客为铺,请各位有兴趣的同学关注. 微信公众号(wx51dotnet):

  10. 老师的blog整理

    python基础部分: 宝哥blog: https://www.cnblogs.com/guobaoyuan/ 开哥blog: https://home.cnblogs.com/u/Neeo 女神笔记 ...