转载 文件断点上传,html5实现前端,java实现服务器

一、单/多文件上传使用例子:

工程路径如下

-src
 |--main.java
    --controller
    --service
    |--config
       --ImageStorageProperties.java
       --WebAppConfig.java
 |--resource
    |--config
       --image-storage.properties

1.controller层

@CrossOrigin("*")
@RequestMapping(value = "/putImages",method = RequestMethod.POST, headers = "Accept=application/json")
public String putImages(@RequestBody MultipartFile[] files, HttpServletRequest request) {
return putImgService.putImages(files,request);
}

2.service层

/**
* 图片单/多张上传
* @param files 文件
* @return
*/
public String putImages(MultipartFile[] files, HttpServletRequest request) {
String imgName = "";
if (files == null || files.length == 0) {
return "图片为空";
}
//遍历并保存文件
for (MultipartFile file : files) {
// 图片传到服务器
imgName=imgUpload(file);
if ("".equals(imgName)) {
return "图片上传失败";
}
}
return "图片上传成功";
} /**
* 图片传到服务器
* @param file
* @return 图片路径
*/
public String imgUpload(MultipartFile file) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
fileName = UUID.randomUUID() + suffixName;
// 图片存储地址,例如"E:/imagesServer/"
String parent = imageStorageProperties.getStoreUrl();
String imgName = "";
try {
File targetFile = new File(parent, fileName);
// 创建文件夹
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
// 将上传文件存储到服务器中
file.transferTo(targetFile);
// 背景图片地址
imgName = targetFile.getName(); // 图片显示地址,例如"http://localhost:8080/imgFiles/" + imgName
imgName = imageStorageProperties.getHttpUrl() + imgName;
System.out.println(imgName);
} catch (IOException e) {
e.printStackTrace();
}
return imgName;
}

3.文件存储路径配置

1)image-storage.properties配置文件如下

image.httpUrl=http://localhost:8080/imgFiles/
image.storeUrl=E:/imagesServer/

2)ImageStorageProperties.java配置类如下

@ConfigurationProperties(prefix = "image", ignoreUnknownFields = false)
@PropertySource(value = {"classpath:config/image-storage.properties"},encoding="utf-8")
public class ImageStorageProperties { private String httpUrl;
private String storeUrl; public String getHttpUrl() {
return httpUrl;
} public void setHttpUrl(String httpUrl) {
this.httpUrl = httpUrl;
} public String getStoreUrl() {
return storeUrl;
} public void setStoreUrl(String storeUrl) {
this.storeUrl = storeUrl;
}
}

4.拦截器配置

WebAppConfig.java配置如下

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter { @Autowired
private ImageStorageProperties imageStorageProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/imgFiles/**").addResourceLocations("file:"+imageStorageProperties.getStoreUrl());
super.addResourceHandlers(registry);
}
}

文件上传之MultipartFile使用的更多相关文章

  1. 文件上传之 MultipartFile

    利用MultipartFile(组件)实现文件上传 在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的Mult ...

  2. 文件上传api——MultipartFile

    MultipartFile 方法总结  byte[] getBytes() 返回文件的内容作为一个字节数组.  String getContentType() 返回文件的内容类型.  InputStr ...

  3. MultipartFile 多文件上传的应用

    公司的项目很多地方要用到文件上传,以前的上传主要是用apache的fileupload ,使用的感受并不太好.今天试了试spring的MultipartFile,感觉还不错,封装的比较简洁. 当然,中 ...

  4. springmvc图片文件上传接口

    springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...

  5. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

  6. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  7. Springboot文件上传代码笔记

    1.在src下创建filter包,包内Class名UploadFilter package com.gd.filter; import org.apache.catalina.servlet4prev ...

  8. javaweb简单的实现文件上传

    java代码: // @RequestMapping(value = "/upload.do", method = RequestMethod.POST) @RequestMapp ...

  9. SpringMVC国际化与文件上传

    点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致. 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 <!-- 注册国际化信息,必 ...

随机推荐

  1. wcf远程服务器返回错误404

    最近根据quartz.net 和wcf做资讯内容定时推送,wcf调用的时候出现远程服务器返回错误404,一直找不到原因是什么,客户端和服务器地址和配置都没啥问题,最后发现wcf请求数据,有传输大小限制 ...

  2. 1px边框的渐变

    <div class="box">banner</div> .box { width: 100px; height: 50px; line-height: ...

  3. 第二十六天- C/S架构 通信流程 socket

    1.C/S架构 C/S架构:Client与Server ,中文意思:客户端与服务器端架构,这种架构也是从用户层面(也可是物理层面)来划分的.这里客户端一般指需先安装再执行的应用程序,对操作系统依赖性较 ...

  4. HTML5实现输入密码(六个格子)

    我的思路:用六个li充当六个格子,同时将input框隐藏,点击承载六个格子的容器时,使焦点聚焦在input上,可以输入.通过监听input框输入的长度,控制格子内小黑点是否显示,同时用正则替换非数字. ...

  5. 2003 - Cann't connect to MySql server on - 'localhost'(10061)

    打开Navicat,打开连接失败,想必大家也会遇到这样的问题,错误消息提示如下: 解决方案如下:首先去看一下数据库服务是否开启,查看方式如下.1.打开任务管理器, oracle数据库服务 mysql数 ...

  6. html基础-表格-列表(4)

    今天准备为大家准备了表格和列表. 一.文章有各种数据的表格这个网页也不例外. (1).标签意思 <table>----------------------表格开始 <caption& ...

  7. AR中的SLAM(一)

    写在前面 本系列打算讲讲个人对AR行业和AR中的SLAM算法的一点浅显的看法.才疏学浅,文中必然有很多疏漏和不足,还望能和大家多多讨论.今天先讲讲我对AR的一些认识. AR的一点理解 AR是什么 AR ...

  8. Python 导出导入安装包

    python导出安装包 pip freeze > requirements.txt python导入安装包 pip install -r requirements.txt

  9. 整理JavaScript循环数组和对象的方法

    众所周知,常用的循环语句有for.while.do-while.for-in,forEach以及jQuery中提供的循环的方法:以及ES6中提供的很多用来循环对象的方法如map, 在 Javascri ...

  10. Android UI组件----自定义ListView实现动态刷新

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...