使用步骤

  1、上传下载需要的依赖

  2、springmvc中配置多媒体解析器并加载

    <!-- 配置多媒体解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件格式-->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean> //扫描application.properties文件
<context:property-placeholder location="classpath:config/application.properties" />
application.properties配置文件
//application.properties文件中的数据
FILE_SERVER_URL=http://192.168.200.128/

 3、前端页面中的文件上传的规范

this.uploadFile = function(){
// 向后台传递数据:
var formData = new FormData();
// 向formData中添加数据:
formData.append("file",file.files[0]);
return $http({
method:'post',
url:'../upload/uploadFile.do',
data:formData,
//添加头信息,浏览器会帮我们吧Content-Type设置为multipart/form-data,.
headers:{'Content-Type':undefined} ,// Content-Type : text/html text/plain
transformRequest: angular.identity
});
}

  4、后台接口代码

@RestController
@RequestMapping("/upload")
public class UploadController {
@Value("${FILE_SERVER_URL}") //从spring容器中的到fastDFS的服务地址
private String FILE_SERVER_URL;
@RequestMapping("/uploadFile")
public Result uploadFile(MultipartFile file){
try {
String filename = file.getOriginalFilename();
//获取文件名的后缀
String extName = filename.substring(filename.indexOf(".1") + 1);
//编译时期异常,用try catch捕获
FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
//服务器中的存放路径地址
String s = fastDFSClient.uploadFile(file.getBytes(), extName);
//将虚拟机的ip地址,和文件服务器端返回的存储地址返回到前端
String url = FILE_SERVER_URL+s;
return new Result(true,url);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"上传失败");
}
}
}

  5、上面我将FastDFS封装成了一个工具类

public class FastDFSClient {

    private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
} public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
} public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
} public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
} public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}

  6、后台将整合好的url传给前端,前端通过

$scope.uploadFile = function(){
// 调用uploadService的方法完成文件的上传
uploadService.uploadFile().success(function(response){
if(response.success){
// 成功的话获得url,不弹窗口
$scope.image_entity.url = response.message;
}else{
//失败的话弹窗
alert(response.message);
}
});
}

去展示我们上传的照片。

Angular中上传图片到分布式文件服务器FastDFS上的更多相关文章

  1. 分布式文件服务器FastDFS的使用

    分布式项目中涉及到的文件上传与下载,此时使用之前的上传方式,将上传的文件与当前项目所在服务器放在同一个位置,显然不符合分布式项目的理念,此时我们借助FastDFS将上传的文件数据存储到单纯的一个服务器 ...

  2. FastDFS分布式文件服务器

    5.分布式文件服务器FastDFS(阿里巴巴) 5.1什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡 ...

  3. ASP.NET中上传图片检测其是否为真实的图片 防范病毒上传至服务器

    一.需求 我们在用.net开发网站时,经常会用到图片上传,可以说是每个网站必备的,大到门户网站,电商网站,政务系统,OA系统,小到企业网站,个人网站,博客网站,导航网站等等,都有用到图片上传,那么在客 ...

  4. 分布式文件系统FastDFS简介、搭建、与SpringBoot整合实现图片上传

    之前大学时搭建过一个FastDFS的图片服务器,当时只是抱着好奇的态度搭着玩一下,当时搭建采用了一台虚拟机,tracker和storage服务在一台机器上放着,最近翻之前的博客突然想着在两台机器上搭建 ...

  5. 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试

    也不说废话,直接干 上一篇 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 中安装了 FastDFS 后,并配置启动了 Tracker 和 Storage 服务,已经可以上传文件 ...

  6. angular下H5上传图片(可多张上传)

    最近做的项目中用到了angular下上传图片功能,在做的过程中遇到了许多问题,最终都得以解决 angular上传时和普通上传时过程差不多,只不过是要不一些东西转化为angular的东西. 1.ng-f ...

  7. 分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

    1. 概述 FastDFS 是目前比较流行的分布式文件系统,可以很容易的实现横向扩展.动态扩容.灾备.高可用和负载均衡. FastDFS 的服务分为 tracker 服务 和 storage 服务,  ...

  8. ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案

    摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...

  9. (转)淘淘商城系列——分布式文件系统FastDFS

    http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...

随机推荐

  1. cmd 运行bcp 提示:'bcp' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    这个问题的原因是:bcp.exe文件的路径不在环境变量中, 我的环境:Windows10 ,SQL server2016(D:) 1.首先查找你的SQL Server2016的安装位置 找到快捷方式, ...

  2. Python collectioins

    collections是一个python的内建模块,提供了一些除了dict.list.tuble.等常见的数据类型之外的一些集合类 参考链接:https://www.liaoxuefeng.com/w ...

  3. Lucene搜索/索引过程笔记

    lucene索引文档过程: > 初始化IndexWriter > 构建Document > 调用IndexWriter.addDocument执行写入 > 初始化Documen ...

  4. .NET开发框架(六)-架构设计之IIS负载均衡(视频)

    前面有关注我们公众号文章的朋友应该都知道,我们的分布式应用服务可以通过Ocelot网关进行负载均衡,这种方式属于应用级别的实现. 而今天我们给大家介绍的是平台级别的实现,并且我们首次使用视频方式进行讲 ...

  5. sess文件编译输出css的四种方式以及使用

    sess文件输出css有下面四种方式: :nested(嵌套) :compact(紧凑) :expanded(展开) :compressed(压缩) 如何使用: sass --watch style. ...

  6. 章节十四、5- web页面的截图

    一.以雅虎网站为例,当我们在登录时,输入错误的用户名然后点击“下一步”,用户名输入框会提示红色字体,这个时候我们就将页面进行截图. http://commons.apache.org/proper/c ...

  7. 解决Vue调用springboot接口403跨域问题

    最近在做一个前后端分离的项目, 前端用的是Vue后端使用的是springboot, 在项目整合的时候发现前端调用后端接口报错403跨域请求问题 前端跨域请求已解决, 那么问题就出在后端了, 找了一些资 ...

  8. (八)OpenStack---M版---双节点搭建---Cinder安装和配置

    ↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 1.创建数据库并授权 2.获得admin凭证执行管理员命令并创建服务证书 3.创建块存储设备AP ...

  9. 题解:[ZJOI2014]璀灿光华

    原题链接 OJ 题号 洛谷 3342 loj 2203 bzoj 3619 题目描述 金先生有一个女朋友没名字.她勤劳勇敢.智慧善良.金先生很喜欢她.为此,金先生用\(a^3\)块\(1 \times ...

  10. 测试脚本中的等待方法 alter对话框处理

    测试脚本中的等待方法 等待是为了使脚本执行更加稳定 1. 常用的休眠方式:time模块的sleep方法 2. selenium模块中的等待方法 等待查找5s 查找不到就报错 对登录测试py进行修改 a ...