java ajax 文件上传_利用SpringMVC和Ajax实现文件上传功能

个人根据相关资料实现利用SpringMVC和Ajax实现文件上传功能:

环境:

1.JDK1.7

2.maven3.3.9

3.Tomcat7

第一步:

导入相关jar包:

3ddd5526b11e5fe314d3d1febc4e36b9.png

第二步:

配置springmvc-config.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

第三步:

配置web.xml

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

id="WebApp_ID" version="3.1">

fileupload

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-config.xml

1

springDispatcherServlet

/

第四步:

新建一个Controller类,并实现文件上传的功能

import java.io.File;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

import javax.json.Json;

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;

import com.alibaba.fastjson.JSON;

import com.fasterxml.jackson.databind.util.JSONPObject;

@Controller

public class FileUploadController {

@RequestMapping(value = "index", method = RequestMethod.GET)

public String index() {

return "index";

}

@RequestMapping(value = "/upload", method = RequestMethod.POST)

@ResponseBody

public String upload(@RequestParam("file") MultipartFile file,

HttpServletRequest request) {

Map modelMap = new HashMap<>();

if (!file.isEmpty()) {

String storePath = "E://images";

Random r = new Random();

String fileName = file.getOriginalFilename();

String[] split = fileName.split(".jpg");

fileName = split[0] + r.nextInt(1000);

fileName = fileName + ".jpg";

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("back", "error");

String json = JSON.toJSONString(modelMap);

return json;

}

modelMap.put("back", "success");

} else {

modelMap.put("back", "error");

}

String json = JSON.toJSONString(modelMap);

return json;

}

}

第五步:

在WEB-INF下,新建一个pages文件夹,并创建实现文件上传的jsp或者HTML文件(我使用的是jsp):

68cc63c3bf8d8d173a934d2e115b94bd.png

在index.jsp下写入相关的ajax的方法,在使用ajax之前必须先导入js库。

$(function() {

$('#upload').click(function() {

var formData = new FormData($('#uploadForm')[0]);

$.ajax({

type : 'POST',

url : 'upload',

data : formData,

cache : false,

processData : false,

contentType : false,

}).success(function(data) {

var result = JSON.parse(data);

alert(result.back);

}).error(function() {

alert("上传失败");

});

});

});

第六步:

进行测试

74e3eecf1d44fda0b183075f426ec6a2.png

上传文件

de45f4bc80233c57292f951a842c202b.png

上传成功

431f211fbe223035324253a3948d4e95.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值