javawebspring-mvc文件上功能

相关文章:
AOP的相关的文章介绍:
javaweb中Aop(jdk动态代理)https://blog.csdn.net/weixin_43319279/article/details/103125051
javaweb中CGLIB动态代理
https://blog.csdn.net/weixin_43319279/article/details/103135053

javaweb中声明式 spring- AOP本文链接:https://blog.csdn.net/weixin_43319279/article/details/103138788

配置pom.xml文件 添加包

	<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.4</version>
	</dependency>

创建上传的控制类(控制上传 ,如要上传到什么目录,最大和最小的大小文件,asp,木马文件!)

package org.business;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
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;

@Controller
public class FileUploadController {
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Map upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		Map<String, String> modelMap = new HashMap<String, String>();
		if (!file.isEmpty()) {
			String storePath = "D://Uploadfiles";
			Random r = new Random();
			String fileName = file.getOriginalFilename();
			String[] split = fileName.split("\\.");
			fileName = split[0] + r.nextInt(1000);
			fileName = fileName + "." + split[1];
			File filePath = new File(storePath, fileName);
			if (!filePath.getParentFile().exists()) {
				filePath.getParentFile().mkdirs();
			}
			try {
				file.transferTo(new File(storePath + File.separator + fileName));
			} catch (Exception e) {
				e.printStackTrace();
				modelMap.put("state", "0");
				modelMap.put("msg", "上传异常!");
			}
			modelMap.put("state", "1");
		} else {
			modelMap.put("msg", "上传失败!");
			modelMap.put("state", "0");
		}
		return modelMap;
	}

}

配置spring-mvc.xml( 配置上传控制器 )

	<!-- 配置上传控制器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="17367648787"></property>
		<property name="defaultEncoding" value="UTF-8"></property>
	</bean>

创建前台页面:
(这个文件要放在webapp下)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传页面</title>
<script type="text/javascript"  src="https://code.jquery.com/jquery-3.4.1.min.js"> </script> 
</head>
<body>
<form id="uploadform" enctype="multipary/farm-data" method="post">
	<input type="file"  name="file">
    <bt/>
    <input type="button"  id="upload" value="上传">
</form>

<script type="text/javascript">
	 $(function(){
		$("#upload").click(function(){
			var  formData = new FormData($("#uploadform")[0]);
			$.ajax({
				type:"POST",
				url:"upload.do",
				data:formData,
				cache:false,
				processData:false,
				contentType:false,
				success:function(data){
					if(data.state==1){
						alert("success!");
					}else{ 
						alert(data.msg);
					} 
				}
			})
		}); 
	 });
</script>
</body>
</html>

结果为:

在这里插入图片描述
查看目录下 的文件夹 下发现 上传的文件。
在这里插入图片描述

发布了80 篇原创文章 · 获赞 15 · 访问量 1866

猜你喜欢

转载自blog.csdn.net/weixin_43319279/article/details/103196088
今日推荐