最近碰到一个上传文件的需求,其实之前也做过但是都是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. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  2. TypeScript开发ReactNative之fetch函数的提示问题

    使用TypeScript开发ReactNative时,发现在类中调用 fetch 函数时IDE可能会提示找不到,无法加载,特别是当类中存在同名的 fetch 成员方法时更是郁闷了,虽然程序是可以执行的 ...

  3. Raspberry Pi(树莓派)上从零开始构建Linux系统(简称PiLFS)(一)

    一. 准备工作 1. 装有Linux宿主系统的树莓派主板,可参考 Raspberry Pi(树莓派)上安装Raspbian(无路由器,无显示器) 2. 参考网址:Linux From Scratch ...

  4. [HMLY]14.对iOS开发中使用MVVM的理解和使用(初级)

    前言 MVVMDemo 之前几个月一直在学习react-native,它的组件化开发真的是很棒,控件和页面的组件化在开发中可以很好的复用,节省开发时间.在那个时候还不知道react-native开发用 ...

  5. Sql Server 日期格式化

    select CONVERT(date,myDtae,23),ID from myFirstTable; http://www.cnblogs.com/hantianwei/archive/2009/ ...

  6. 在使用cognos时遇到的问题记录帖

    在使用cognos时遇到的问题记录帖 1.开一个project 报无法访问位于 URL 的服务: http://localhost:80/ibmcognos/cgi-bin/cognos.cgi?b_ ...

  7. Noip 2016

    Day1 思路: 大致是 把一个环拆成链, 找某个人无非是向右找或向左找(即对当前点加或减) 若加上要移动的位置后坐标大于总人数, 就把当前坐标减去总人数, 若减去要移动的位置后坐标小于0, 就把当前 ...

  8. Python学习笔记进阶篇——总览

    Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...

  9. centos 编译安装nginx

    这里选用的是nginx-1.10.1稳定版,其基础依赖库有gcc.gcc-c++.pcre.zlib和openssl. pcre.zlib和openssl这三个依赖库在安装nginx时无需编译安装,下 ...

  10. C#调用winhttp组件 POST登录迅雷

    下面是封装好的winhttp类 using System; using System.Collections.Generic; using System.Linq; using System.Text ...