最近碰到一个上传文件的需求,其实之前也做过但是都是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. 8259A工作原理描述

    通过初始化编程向8259A写入相应的初始化命令ICW,可以使芯片处于一个规定的基本工作方式,并在此方式下进行工作.8259A的初始化命令字共有4个ICW1-ICW4,进行初始化时要求ICW1-ICW4 ...

  2. 【CSS学习笔记】超链接标签

    有些网址后面为什么是#? 比如,href="http://www.xxx.com/index.html/#q2"标示网页index.html的q2位置处,浏览器读取这个URL后,会 ...

  3. linux ftp安装和配置

    1. 启动VSFTP服务器 A:cenos下运行:yum  install  vsftpd B. 登录Linux主机后,运行命令:"service vsftpd start"C. ...

  4. 第一百二十六节,JavaScript,XPath操作xml节点

    第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...

  5. C#字符串和16进制转换

    public static string StrToHex(string mStr) //返回处理后的十六进制字符串 { return BitConverter.ToString( ASCIIEnco ...

  6. [UWP小白日记-8]一些零碎的东西

    设置启动窗口大小 直接上代码了没什么好解释的了,既然能设置最小,那铁定就能设置最大 public MainPage() { //设定窗口启动显示大小 ApplicationView.Preferred ...

  7. typings 命令使用注意

    1.如果要查询一些库 typings search xxx 2.安装jquery node 这样的库要这样 typings dt~node --global --save   一定要dt~xxx ,然 ...

  8. 【游记】NOIP2015造纸记

    题目来自HZWER学长的名言:“虽然已经做好了学OI就是打铁的准备.” 然后我发现我已经不是打铁,只能造纸了啊_(:3LZ_) [DAY0] 中午吃了饭才1:00,说好2:30才出发于是各种闲逛.2: ...

  9. My Eclipse Security Alert

    SECURITY ALERT: INTEGRITY CHECK ERROR This product did not pass the MyEclipse integrity check. This ...

  10. 替换ubuntu 14.04的源

    1. 背景(为什么要替换)安装ubuntu,默认源是(http://extras.ubuntu.com/ubuntu),国内访问很慢...当我们用apt-get安装软件包或者更新时有时很慢,所以才想到 ...