Spring mvc上传文件 demo

1、准备工作有springmvc环境
2、加载maven包

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
		    <groupId>commons-io</groupId>
		    <artifactId>commons-io</artifactId>
		    <version>2.6</version>
		</dependency>

3、配置spring-mvc

<!-- 文件上传bean -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

4、interface

package com.spring.myfileutilsinterface;

import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

public interface IFileUpDownLoadInterface {
	
	public boolean fileUpload(MultipartFile mf,String path,String name)throws IllegalStateException, IOException ;
	
}

5、implements

package com.spring.myfileutilsimplements;

import java.io.File;
import java.io.IOException;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.spring.myfileutilsinterface.IFileUpDownLoadInterface;

@Service("fileUpDownLoadImpl")
public class FileUpDownLoadImpl implements IFileUpDownLoadInterface {

	/**
	 * 简单的上传文件
	 * mf 文件
	 * path 文件路径 
	 * name 必须有后缀
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	public boolean fileUpload(MultipartFile mf, String path,String name) throws IllegalStateException, IOException {
		File filePath = null;
		//判断文件是否为空
		if(!mf.isEmpty()) {
			//判断path 是不是为null
			if(path!=null && path.length() != 0) {
				filePath = new File(path,name);
			}
		}
		if(filePath == null) {
			return false;
		}
		//判断文件夹是否存在 如果不存在的话  需要新建
		if(!filePath.getParentFile().exists()) {
			filePath.getParentFile().mkdirs();
		}
		mf.transferTo(filePath);
		
		return true;
	}

}

6、controller

package com.spring.test.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
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 com.spring.myfileutilsinterface.IFileUpDownLoadInterface;


@Controller
public class FileUpDownLoadController {
	
	@Autowired
	@Qualifier("fileUpDownLoadImpl")
	private IFileUpDownLoadInterface fileUpDownLoadImpl;
	
	//发布文章
	@RequestMapping(value = "upload", method = { RequestMethod.POST })
	@CrossOrigin
	@ResponseBody
	public String  upload(@RequestParam("file") MultipartFile file,@RequestParam("filename") String filename,HttpServletRequest request){
		String path ="/home/jackray/soft/temp";
		try {
			fileUpDownLoadImpl.fileUpload(file, path, filename);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

7.前端

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

<form action="upload" enctype="multipart/form-data" method="post">
	<table>
		<tr>
			<td>文件名称</td>
			<td><input type="text" name="filename"></td>
		</tr>
		<tr>
			<td>请选择文件</td>
			<td><input type="file" name="file"></td>
		</tr>
		<tr>
			<td><input type="submit" value="上传"></td>		
		</tr>
	</table>
</form>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值