最近碰到一个上传文件的需求,其实之前也做过但是都是search->copy 没有细究过,这次纯手工。

先看一下需要依赖的包:

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>

然后看一下bean的配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!--代表上传文件大小的最大值 -1代表无限大-->
<property name="maxUploadSize" value="-1"/>
<!--如果文件大小小于maxInMemorySize 的时候 系统不会产生临时文件 直接将文件写在内存中 需要注意-->
<property name="maxInMemorySize" value="1"/>
</bean>

接下来看一下 controller层  自己随意写的 轻喷

package com.springapp.mvc;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import java.io.File;
import java.io.IOException; /**
* Simple to Introduction
*/
@Controller
public class FileController { @RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody void uploadFile(@RequestParam(value = "file") MultipartFile multipartFile, Model model) throws IOException {
CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
String content = FileUtils.readFileToString(file);
model.addAttribute("content", content);
}
}

上面我进行了一下文件转换,读了一下文件内容

接下来看一下前端  原生form的

     <form id="fileuploadForm" action="/upload" method="POST" enctype="multipart/form-data" class="cleanform">
<input id="file" type="file" name="file" />
<p><button type="submit">Upload</button></p>
</form>

再来一发angular 原生的

        <a href="javascript:;" class="btn-small btn-blue in_block" ngf-select ng-model="upLoadFiles"
ng-click="changeStatus">上传</a>

js  app这些就不写了从 controller开始吧 需要注入 Upload

function ConfigAuthController($scope, $rootScope, $http, Upload) {

/**
* 开始上传
*/
function importFile() { $scope.showPop = false;
var files = $scope.upLoadFiles;
console.log(files); if (!files || files.length == 0) {
$scope.message = "请选择文件";
return false;
} for (var i = 0; i < files.length; i++) {
$scope.loadStatus = true;
var file = files[i];
if (file.type != "text/plain") {
$scope.message1="";
$scope.message = "请上传文件TXT格式";
showPopupDiv($('#layer_warning'));
return;
}
if (file.size > 5 * 1024 * 1024) {
$scope.message1="";
$scope.message = "上传的文件大小超过5M";
showPopupDiv($('#layer_warning'));
return;
}
//if($scope.workspaceEmpFilePath.checkStatus == false){
// $scope.message = "文件ID格式错误";
// showPopupDiv($('#layer_warning'));
// return;
//} Upload.upload({
url: '/workspaceAuth/upload',
file: file,
fileFormDataName: 'uploadFile'
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('进度:' + progressPercentage + '% 文件名:' + evt.config.file.name);
}).success(function (data, status, headers, config) { if (data.checkStatus == false) {
//
return;
}
$scope.getFile = data;
alert("上传成功!")
}).error(function (data, status, headers, config) {
$scope.message = data.message;
$scope.loadStatus = false;
});
}
} }

暂时写到这 后续补充

Spring 上传文件的更多相关文章

  1. spring上传文件

    在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...

  2. spring 上传文件文件的一个例子,

    /** * 类名称:UploadTest 类描述:创建人:zhang 创建时间:2015年3月13日 下午4:20:57 修改人:zhang * 修改时间:2015年3月13日 下午4:20:57 修 ...

  3. Spring上传文件,图片,以及常见的问题

    1. 在工程依赖库下添加文件上传jar包 commons-fileupload-1.2.2.jar commons-io-2.4.jar 2.在springMVC配置文件中配置视图解析multipar ...

  4. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  5. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  6. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  7. spring boot(十七)上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  8. 使用Spring Boot上传文件

    原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...

  9. Spring Boot上传文件

    我们使用Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0. <parent> <groupId>org.springframework.boot& ...

随机推荐

  1. 菜单工具栏wxPython菜单与工具栏基础示例

    这两天一直在学习菜单工具栏之类的问题,上午正好有机会和大家讨论一下. 1.基本的api介绍 Package wx :: Class Menu Type Menu Method Summary Menu ...

  2. kubernetes源码阅读及编译

    kubernetes源码阅读 工欲善其事,必先利其器.在阅读kubernetes源码时,我也先后使用过多个IDE,最终还是停留在IDEA上. 我惯用的是pycharm(IDEA的python IDE版 ...

  3. NUTZ中处理系统未捕获异常

    关键内容 mvc-chain.js ViewProcessor ai.setFailView(“redirect:/sysError.html”); log.error(this.trrowableT ...

  4. [ios2] ios7UI适配 【转】

    http://blog.csdn.net/toss156/article/details/11843873#comments (1)如果应用程序始终隐藏 status bar 那么恭喜呢,你在UI上需 ...

  5. Ueditor1.4.3实现跨域上传到独立文件服务器,完美解决单文件和多文件上传!

    再写配置方法之前先吐槽一下网上的各种教程,TM没一个有卵用,一群傻屌不会写就别写,写了就要负责. 百度google搜了半天,全是配置什么document.domain,根域名什么的,我只想对你说: 好 ...

  6. Python_day1 基础语法

    1.基础语法变量: 在左侧自定义输入变量名,右侧可以输入任意类型赋值给左侧,如需制定类型,可以强转name = Jason, age = int(24) provience = ['beijing', ...

  7. Ubuntu16.04 server下配置MySQL,并开启远程连接

    背景 最近正在学nodejs,想到曾经有台云服务器,但是很久不用了,由于怕麻烦,一股脑的把云主机重装了个Ubuntu系统,于是配置MySQL成了配置服务中的一个环节(node用不用MySQL不管,主要 ...

  8. python_login输入三次错误密码锁定密码_密码不允许为空

    #!/usr/bin/env python #_*_coding:utf-8_*_ #by anthor zhangxiaoyu 2017-01-10 import getpass import os ...

  9. 移动开发meta集合【精】

    以下是开发中经常用到的meta标签 1.移动webAPP的Meta 用法: <meta content="width=device-width, initial-scale=1.0, ...

  10. C#:继承多态的方法实现数的简单加减乘除运算

       // 定义一个抽象的父类     abstract class Figure     {         //声明抽象方法:         //父类中的所有家里人可以用的方法必须都应用到子类中 ...