使用步骤

  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. PIE SDK水深提取算法

    1.算法功能简介 水深提取算法就是根据输入的水位设为d,dem设为h 这两个数据做一个差值运算,则水深计算公式为d-h;本示例中的是基于洞庭湖提取的水体矢量文件的范围来计算dem和水位25米的差值. ...

  2. Socket与WebSocket以及http与https重新总结

    Socket与WebSocket以及http与https重新总结 一.Socket 网络中的Socket是一个抽象的接口 ,而是为了方便使用TCP或UDP而抽象出来的一层 ,可以理解为网络中连接的两端 ...

  3. Java自学-集合框架 二叉树

    Java集合框架 二叉树 示例 1 : 二叉树概念 二叉树由各种节点组成 二叉树特点: 每个节点都可以有左子节点,右子节点 每一个节点都有一个值 package collection; public ...

  4. tkinter为多个窗口设置相同的icon报错

    import threading import tkinter from PIL import Image, ImageTk def show_window(): window = tkinter.T ...

  5. Android培训准备资料之UI一些相似控件和控件一些相似属性之间的区别

    这一篇博客主要收集五大布局中的一些相似控件和控件一些相似属性之间的区别 ImageView ImageButton Button 三者有啥区别? (1)Button继承自TextView,ImageV ...

  6. vue 开发系列(十) VUE 作用域插槽

    使用场景 官方解释,有时让插槽内容能够访问子组件中才有的数据是很有用的.比如我们在使用ant-design-vue 的表格控件时. <a-table-column title="注释& ...

  7. vsftpd服务

    vsftpd服务 文件传输协议(file transfer protocol,FTP),基于该协议FTP客户端与服务端可以实现共享文件,上传文件,下载文件.ftp基于TCP协议生成一个虚拟的连接,主要 ...

  8. 201871010102-常龙龙《面向对象程序设计(java)》第七周学习总结

    二.博文正文开头格式: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...

  9. 【转】Spring全家桶

    Spring框架自诞生以来一直备受开发者青睐,有人亲切的称之为:Spring 全家桶.它包括SpringMVC.SpringBoot.Spring Cloud.Spring Cloud Dataflo ...

  10. Nginx的代理配置(六)

    一.正向代理 1. 指令说明 (1) resolver 这个用于设置DNS服务器的ip .DNS服务器的主要工作是进行域名解析,将域名映射为对应IP地址. 语法:resolver address .. ...