最近碰到一个上传文件的需求,其实之前也做过但是都是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. Linux中最受欢迎的邮件传输代理(MTA)

    MTA(Mail transfer Agent) -对于服务器管理员来说,邮件传输代理是最为重要的工具.MTA的主要工作就是将电子邮件从一台主机发送到另一台主机.MTA使用SMTP(简单邮件传输协议) ...

  2. jQuery插件slides实现无缝轮播图特效

    初始化插件: slides是一款基于jQuery无缝轮播图插件,支持图内元素动画,可以自定义动画类型 1 2 3 4 5 6 7 8 9 10 $(".slideInner").s ...

  3. ipad 横屏 竖屏 CSS

    /* iPads (landscape) ----------- */ @media only screen and (min-device-width : 768px) and (max-devic ...

  4. 【多重背包】HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) ...

  5. Objective-C运行时态消息传递--拼接方法名

    做IOS开发的人都知道,Objective-C语言中方法的调用是运行时采取绑定的,在编译过程中只声明该方法的存在. 那么我们来简单说下在运行时,类的消息传递. 在运行时,每个方法如[self meth ...

  6. 十件你需要知道的事,关于openstack-trove(翻译)

    开源数据库即服务OpenStack Trove应该知道的10件事情 作者:Ken Rugg,Tesora首席执行官 Ken Rugg是Tesora的创始人,CEO和董事会成员. Ken的大部分职业都是 ...

  7. 【LeetCode】31. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. c++ 常见问题之string

    string初始化: 拷贝初始化: = 初始化变量,编译器把等号右侧的初始值拷贝到新创建的对象  string s = "stduent";    string s = s1; 直 ...

  9. jquery插件autoComplete自动弹出

    导入 <link rel="stylesheet" href="${ctx }/static/plugins/jQuery-autoComplete-master/ ...

  10. ORACLE AWR性能报告和ASH性能报告的解读

    数据库的性能分析可分为会话级和系统级:如果确定某个会话存在性能问题,最常见的分析方式是对这个会话做一个SQL_TRACE或者10046事件,通过分析trace文件来定位问题所在.如果无法确定哪个会话性 ...