1、需求:通过andserver 查询Android端数据库数据,需要返回图片的URL地址

andserver相关

AndServer的github地址【https://github.com/yanzhenjie/AndServer】
AndServer文档地址【https://www.yanzhenjie.com/AndServer/】
AndServer的相关博客地址【https://blog.csdn.net/yanzhenjie1003/article/details/51661599】比较老

AndServer,一个Android端的web服务器 https://blog.csdn.net/yanzhenjie1003/article/details/64090436

要做一个可以在网页输入地址就可以浏览图片的功能

自定义一个MyFileBrowser

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;import com.yanzhenjie.andserver.annotation.Config;
import com.yanzhenjie.andserver.error.NotFoundException;
import com.yanzhenjie.andserver.framework.body.FileBody;
import com.yanzhenjie.andserver.framework.website.BasicWebsite;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.ResponseBody;
import com.yanzhenjie.andserver.util.Assert;
import com.yanzhenjie.andserver.util.DigestUtils;
import com.yanzhenjie.andserver.util.MediaType;
import com.yanzhenjie.andserver.util.ObjectUtils;
import com.yanzhenjie.andserver.util.Patterns;
import com.yanzhenjie.andserver.util.StringUtils;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;@Config
public class MyFileBrowser extends BasicWebsite implements Patterns {private final String mRootPath;/*** 路径组*/public String[] value = {"faceImg", "faceRecordImg", "cardRecordImg"};/*** 照片格式*/public String[] photo_s = {".jpg"};//路径规则     /storage/emulated/0/tecsunFacepublic MyFileBrowser(String rootPath) {Assert.isTrue(!StringUtils.isEmpty(rootPath), "The rootPath cannot be empty.");Assert.isTrue(rootPath.matches(Patterns.PATH), "The format of [%s] is wrong, it should be like [/root/project].");this.mRootPath = rootPath;}@Overridepublic boolean intercept(@NonNull HttpRequest request) {String httpPath = request.getPath();//  /faceImg/1.jpgFile source = findHttpPathResource(httpPath);return source != null;}/*** 判断是否是正确的照片路径** @param httpPath* @return*/private File findHttpPathResource(@NonNull String httpPath) {//修改后的路径检查String[] strings = httpPath.split("/");if (strings.length != 3) {return getDefault();}String filepath = strings[1];String photo = strings[2];//xxx.jpgif (!isPhoto(photo_s, photo)) {//判断是否是图片return getDefault();}boolean flag = isHave(value, filepath);//判断是否是我定义的路径if (flag) {String path = mRootPath + "/" + filepath + "/" + photo;File root = new File(path);return root.exists() ? root : getDefault();} else {return getDefault();}}/*** 照片后后缀判断** @param photos 照片组* @param photo  xx.jpg* @return boolean*/private boolean isPhoto(String[] photos, String photo) {for (String key : photos) {return photo.endsWith(key);}return false;}/*** 判断某个字符串是否存在于数组中** @param stringArray 原数组* @param source      查找的字符串* @return 是否找到*/public static boolean isHave(String[] stringArray, String source) {/*此方法有两个参数,第一个是要查找的字符串数组,第二个是要查找的字符或字符串*/boolean flag = false;for (String key : stringArray) {if (source.contains(key)) {flag = true;break;}}return flag;}private File getDefault() {return null;}/*** Find the path specified resource.** @param httpPath path.* @return return if the file is found.*/private File findPathResource(@NonNull String httpPath) {//修改前的路径检查if ("/".equals(httpPath)) {File root = new File(mRootPath);return root.exists() ? root : null;} else {File sourceFile = new File(mRootPath, httpPath);if (sourceFile.exists()) {return sourceFile;}}return null;}@Overridepublic String getETag(@NonNull HttpRequest request) throws IOException {String httpPath = request.getPath();File resource = findHttpPathResource(httpPath);if (resource != null) {String tag = resource.getAbsolutePath() + resource.lastModified();return DigestUtils.md5DigestAsHex(tag);}return null;}@Overridepublic long getLastModified(@NonNull HttpRequest request) throws IOException {String httpPath = request.getPath();File resource = findHttpPathResource(httpPath);if (resource != null) {return resource.lastModified();}return -1;}@NonNull@Overridepublic ResponseBody getBody(@NonNull HttpRequest request) throws IOException {String httpPath = request.getPath();File resource = findHttpPathResource(httpPath);if (resource == null) {throw new NotFoundException(httpPath);}if (resource.isDirectory()) {File tempFile = File.createTempFile("file_browser", ".html");OutputStream outputStream = new FileOutputStream(tempFile);String folderName = resource.getName();String prefix = String.format(FOLDER_HTML_PREFIX, folderName, folderName);outputStream.write(prefix.getBytes("utf-8"));File[] children = resource.listFiles();if (!ObjectUtils.isEmpty(children)) {for (File file : children) {String filePath = file.getAbsolutePath();int rootIndex = filePath.indexOf(mRootPath);String subHttpPath = filePath.substring(rootIndex + mRootPath.length());subHttpPath = addStartSlash(subHttpPath);String fileItem = String.format(FOLDER_ITEM, subHttpPath, file.getName());outputStream.write(fileItem.getBytes("utf-8"));}}outputStream.write(FOLDER_HTML_SUFFIX.getBytes("utf-8"));return new FileBody(tempFile) {@Nullable@Overridepublic MediaType contentType() {MediaType mimeType = super.contentType();if (mimeType != null) {mimeType = new MediaType(mimeType.getType(), mimeType.getSubtype(), StandardCharsets.UTF_8);}return mimeType;}};} else {return new FileBody(resource);}}private static final String FOLDER_HTML_PREFIX = "<!DOCTYPE html><html><head><meta http-equiv=\"content-type\" " +"content=\"text/html; charset=utf-8\"/> <meta name=\"viewport\" content=\"width=device-width, " +"initial-scale=1, user-scalable=no\"><metaname=\"format-detection\" content=\"telephone=no\"/> " +"<title>%1$s</title><style>.center_horizontal{margin:0 auto;text-align:center;} *,*::after,*::before " +"{box-sizing: border-box;margin: 0;padding: 0;}a:-webkit-any-link {color: -webkit-link;cursor: auto;" +"text-decoration: underline;}ul {list-style: none;display: block;list-style-type: none;-webkit-margin-before:" +" 1em;-webkit-margin-after: 1em;-webkit-margin-start: 0px;-webkit-margin-end: 0px;-webkit-padding-start: " +"40px;}li {display: list-item;text-align: -webkit-match-parent;margin-bottom: 5px;}</style></head><body><h1 " +"class=\"center_horizontal\">%2$s</h1><ul>";private static final String FOLDER_ITEM = "<li><a href=\"%1$s\">%2$s</a></li>";private static final String FOLDER_HTML_SUFFIX = "</ul></body></html>";

使用MyFileBrowser

import android.content.Context;import com.yanzhenjie.andserver.annotation.Config;
import com.yanzhenjie.andserver.framework.config.Multipart;
import com.yanzhenjie.andserver.framework.config.WebConfig;import java.io.File;@Config
public class AppConfing implements WebConfig {@Overridepublic void onConfig(Context context, Delegate delegate) {delegate.addWebsite(new MyFileBrowser(PathManager.getInstance().getWebDir()));delegate.setMultipart(Multipart.newBuilder().allFileMaxSize(1024 * 1024 * 20) // 20M.fileMaxSize(1024 * 1024 * 5) // 5M.maxInMemorySize(1024 * 10) // 1024 * 10 bytes.uploadTempDir(new File(context.getCacheDir(), "_server_upload_cache_")).build());}
}
public class PathManager {private static PathManager sInstance;public static PathManager getInstance() {if (sInstance == null) {synchronized (PathManager.class) {if (sInstance == null) {sInstance = new PathManager();}}}return sInstance;}private String path;private PathManager() {path = FileUtils.ROOT + FileUtils.DIR;//文件在手机中的路径
//        path = FileUtils.ROOT + FileUtils.faceDIR;FileUtils.createNoMedia(path);}public String getRootDir() {return path;}public String getWebDir() {return path;}
}

andserver FileBrowser 图片浏览相关推荐

  1. 原创“.NET研究”企业级控件库之图片浏览控件

    在上两篇:我介绍了原创企业级控件库之组合查询控件 和原创企业级控件库之大数据量分页控件,受到了很多朋友的支持,给了我很大的动力,在此我特表感谢.有的朋友要求把源码提供上来,我在第一篇就讲了,源码会在我 ...

  2. 图片浏览(CATransition)转场动画

    Main.storyboard ViewController.m // //  ViewController.m //  8A04.图片浏览(转场动画) // //  Created by huan ...

  3. iOS_“图片浏览选择”功能的编写思路

    最近重新开始练习iOS开发,找感觉. 先做个简单的图片浏览选择功能.不用管是难还是简单,先实现一下. 一.步骤概述 包含三个步骤: 创建页面:图片选择页(collectionView),图片预览页 处 ...

  4. 一起撸个朋友圈吧 图片浏览(上)【图片点击前景色】

    项目地址:github.com/razerdp/Fri- (能弱弱的求个star或者fork么QAQ) 上篇链接:一起撸个朋友圈吧 (Step6)- 评论对齐(点击评论对齐)[下] 下篇链接:一起撸个 ...

  5. vscode 预览图片 插件_真的动手写的VSCode的插件(图片浏览)之1

    由于本职工作中经常做图像处理,于时大量的图片浏览是不可避免的. 怎么样不离开最近经常使用的VSCode,同时去看大量的图像对我来讲就是个不错的需求,尤其是某个目录下的文件. 先谈基本的需求吧,显示一个 ...

  6. android ViewPager 图片浏览和保存图片

    在build.gradle在添加依赖 compile 'com.alibaba:fastjson:1.1.54.android' compile 'org.ligboy.retrofit2:conve ...

  7. android图片浏览功能,怎么在Android应用中实现一个网页图片浏览功能

    怎么在Android应用中实现一个网页图片浏览功能 发布时间:2020-12-05 17:28:31 来源:亿速云 阅读:80 作者:Leah 本篇文章给大家分享的是有关怎么在Android应用中实现 ...

  8. 表单-图片浏览上传-单选框(二)

    一.图片浏览上传 1.依然[table]标签包含, 2.[input]包含了[type]等于[file]. <table border="1" align="cen ...

  9. jquery插件图片浏览

    jquery插件图片浏览 jquery代码部分 (function($){$.fn.mPicsList = function(options){var picsImgs = $(this).find( ...

  10. 模仿微信朋友圈 图片浏览 js javascript 支持图片预览,滑动切换,双指缩放,图片缓存

    模仿微信朋友圈 图片浏览 js javascript 支持图片预览,滑动切换,双指缩放,图片缓存 2017年08月10日 12:11:38 阅读数:2311 previewImage-mobile 仿 ...

最新文章

  1. php 自加 性能,对于数据库的自增、自减心得
  2. matlab bfs函数,matlab练习程序(广度优先搜索BFS、深度优先搜索DFS)
  3. 面试干货——年底干货大放送,你准备好了吗?
  4. matlab柱状斜线_Matlab:柱状图饼状图填充不同条纹
  5. Squid正向代理矩阵
  6. 从LiveVideoStackCon 2019北京看多媒体技术趋势
  7. .NET Core技术研究-主机
  8. django-查询-F对象-Q对象
  9. 正在中止线程 iis_Zephyr线程生命周期及状态
  10. .Net程序员学用Oracle系列(16):访问数据库(ODP.NET)
  11. 报送数据标准校验java_Java:数据校验 - osc_gaqp1a2z的个人空间 - OSCHINA - 中文开源技术交流社区...
  12. 2011浙大878计算机专业基础综合大题答案解析
  13. python项目依赖库打包发布方法
  14. 可公度线段与欧几里得(Euclid)算法
  15. hashmap-put方法过程
  16. swift UI专项训练19 TextView 多行文本
  17. UVa 10129 - Play on Words (欧拉回路, DFS)
  18. 模板字符串竟然还有这种用法
  19. verilog逻辑符
  20. 利用echarts做堆积折线图

热门文章

  1. python中不同文件之间使用所谓的全局变量
  2. 计算机基础知识初中生学习,初中生具备了学习电脑的能力吗?
  3. 新连接、新生意、新生态,专访快手商业生态开放平台
  4. 贝多芬《 d小调第九交响曲》(Symphony No.9 in d Minor, Op.125, 1824)(永无完结)
  5. 贝多芬交响曲全集(转)
  6. (python)爬虫----八个项目带你进入爬虫的世界
  7. 解析 URP 教务系统, 创建查成绩 APP !
  8. showModalDialog()、showModelessDialog()方法使用详解
  9. SQL 的各种 join 用法
  10. 浏览器显示域名解析错误怎么办