Angular中上传图片到分布式文件服务器FastDFS上
使用步骤
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上的更多相关文章
- 分布式文件服务器FastDFS的使用
分布式项目中涉及到的文件上传与下载,此时使用之前的上传方式,将上传的文件与当前项目所在服务器放在同一个位置,显然不符合分布式项目的理念,此时我们借助FastDFS将上传的文件数据存储到单纯的一个服务器 ...
- FastDFS分布式文件服务器
5.分布式文件服务器FastDFS(阿里巴巴) 5.1什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡 ...
- ASP.NET中上传图片检测其是否为真实的图片 防范病毒上传至服务器
一.需求 我们在用.net开发网站时,经常会用到图片上传,可以说是每个网站必备的,大到门户网站,电商网站,政务系统,OA系统,小到企业网站,个人网站,博客网站,导航网站等等,都有用到图片上传,那么在客 ...
- 分布式文件系统FastDFS简介、搭建、与SpringBoot整合实现图片上传
之前大学时搭建过一个FastDFS的图片服务器,当时只是抱着好奇的态度搭着玩一下,当时搭建采用了一台虚拟机,tracker和storage服务在一台机器上放着,最近翻之前的博客突然想着在两台机器上搭建 ...
- 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试
也不说废话,直接干 上一篇 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 中安装了 FastDFS 后,并配置启动了 Tracker 和 Storage 服务,已经可以上传文件 ...
- angular下H5上传图片(可多张上传)
最近做的项目中用到了angular下上传图片功能,在做的过程中遇到了许多问题,最终都得以解决 angular上传时和普通上传时过程差不多,只不过是要不一些东西转化为angular的东西. 1.ng-f ...
- 分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合
1. 概述 FastDFS 是目前比较流行的分布式文件系统,可以很容易的实现横向扩展.动态扩容.灾备.高可用和负载均衡. FastDFS 的服务分为 tracker 服务 和 storage 服务, ...
- ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案
摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...
- (转)淘淘商城系列——分布式文件系统FastDFS
http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...
随机推荐
- JDK1.8新特性——Stream API
JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...
- AES加解密异常java.security.InvalidKeyException: Illegal key size
AES加解密异常 Java后台AES解密,抛出异常如下:java.security.InvalidKeyException: Illegal key size Illegal key size or ...
- 回忆C++
内联函数 内联函数适用于函数较为短小的情况. 内联函数存在的意义是:提高程序运行效率. 内联函数的缺点:如果一个内联函数太长且频繁调用,会导致生成的可执行程序较大. 静态链接库会被嵌入到生成的可执行程 ...
- 学习shiro第三天
今天比较晚,所以只看了shiro的认证策略Authentication Strategy,下面讲讲shiro的三种认证策略. 1.AtLeastOneSuccessfulStrategy:这个是shi ...
- 再谈EFAGE寄存器中的C位,P位,O位
由于写EFLAGE博文中,有关C位,P位,O位,我觉得我没有描述清楚,而且C位也没有演示过借位情况,P位中也有些坑没讲,我还是决定再补一篇,争取把每个标志位描述清楚,不光是让看我文章的人能看的明白,也 ...
- 软工Alpha七天冲刺
七天冲刺博客: 1.第一篇Scrum冲刺博客 2.第二篇Scrum冲刺博客 3.第三篇Scrum冲刺博客 4.第四篇Scrum冲刺博客 5.第五篇Scrum冲刺博客 6.第六篇Scrum冲刺博客 7. ...
- windows及linux环境下修改pip的默认镜像源的方法
1. 在windows环境下 临时修改 使用清华大学的源安装numpy包. pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple ...
- 第17节-BLE安全管理概述
安全管理是BLE中最复杂的内容,涉及LL层.SM层.GAP层 一.妈妈的担心 1. 白名单: 妈妈说,你只能跟A.B.C这3个好孩子玩:他们打电话给你,你才可以出去玩. A.B.C三人,就在妈妈的“白 ...
- 201871020225-牟星源《面向对象程序设计(java)》第八周学习总结
201871020225-牟星源<面向对象程序设计(java)>第八周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这 ...
- LiunxCentos7 上安装 FastDFS
Centos7 上安装 FastDFS 1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gc ...