转载 文件断点上传,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. spring-boot集成PageHelper和通用Mapper

    前提条件:已经集成mybatis 代码生成步骤: 添加依赖 <dependency> <groupId>tk.mybatis</groupId> <artif ...

  2. Code Signal_练习题_reverseParentheses

    You have a string s that consists of English letters, punctuation marks, whitespace characters, and ...

  3. css画一个提示框

    用css画一个如下图的提示框: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. js面向对象设计之function类

    本文仅探讨如何合理的使用 function 在 javascript中实现一个面向对象设计的类.总所周知,javascript 并不能实现一个真正意义上的类,比如 protect 比如 函数重载.下面 ...

  5. 以local模式使用Xshell+Xmanager远程监控jvisualvm

    使用jvisualvm的remote方式监控服务器端jvisualvm时,不是很方便,因此通过local方式,应该是正路. 一.服务器端(Linux,最小安装模式,没有图形界面) 1.安装xauth ...

  6. 用C#自定义一个简单的集合

    闲来无聊来自己做了一个简单的'集合',用来加深自己对集合的理解 class listNode { private object value; public listNode(object _value ...

  7. 基于dispatch_after封装YXTimer

    基于dispatch_after封装YXTimer 本人根据dispatch_after封装了一个定时器,支持block以及代理的方式来激活定时器,适用于对精度要求低,耗时短的地方,高端大气上档次,低 ...

  8. Inside Amazon's Kafkaesque "Performance Improvement Plans"

    Amazon CEO and brilliant prick Jeff Bezos seems to have lost his magic touch lately. Investors, empl ...

  9. Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业(线性回归)

    一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...

  10. Skype for Business Server 2015 企业语音部署和配置

    Skype for Business Server 2015包含的企业语音功能可实现更丰富的通信和协作.例如,可以将企业语音部署配置为启用Skype for Business Server 2015客 ...