转载 文件断点上传,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. easyUi datagrid鼠标经过提示单元格内容

    此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. jquery.cellTip.js /** * 扩展两个方法 */ using('datagrid', function() ...

  2. future3.2 Tomcat启动时错误:Cannot rename original file to ...

    其日志中第一个警告如下: 警告: Unexpected exception resolving reference java.io.IOException: Cannot rename origina ...

  3. js-权威指南学习笔记17

    第十七章 事件处理 1.事件处理程序或事件监听程序是处理或响应事件的函数. 2.事件对象是与特定事件相关且包含有关该事件详细信息的对象. 3.响应通过键盘改变焦点的表单元素在得到和失去焦点时会分别出发 ...

  4. Python之dict和set

    dict Python内置了字典:dict的支持,dict全称dictionary,使用键-值(key-value)存储,具有极快的查找速度 1.例如:查找某位同学对应的成绩,使用“名字”-“成绩”的 ...

  5. 国内阿里云Maven镜像(速度飞起)

    修改maven根目录下的conf文件夹中的setting.xml文件,内容如下: <mirrors> <mirror> <id>alimaven</id> ...

  6. React-Native开发之原生模块封装(Android)升级版

     本文主题:如何实现原生代码的复用,即如何将原生模块封装. (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/52862 ...

  7. windows 7 Alt+Tab 的风格改成 XP 风格

    1.开始菜单-运行-输入“regedit”. 2.找到这个位置“[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explore ...

  8. springMVC结合AjaxForm上传文件

    最近在项目中需要上传文件文件,之前一直都是form提交的,尝试了一下AjaxForm,感觉还比较好用,写篇随笔mark下,供以后使用. 准备工作: 下载jquery-form.js 相关jar: co ...

  9. [翻译] JKLLockScreenViewController

    JKLLockScreenViewController https://github.com/tiny2n/JKLLockScreenViewController Overview It is Loc ...

  10. [翻译] TransitionKit

    TransitionKit https://github.com/blakewatters/TransitionKit A simple, elegantly designed block based ...