欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用SpringMVC和Ajax實現(xiàn)文件上傳功能

 更新時間:2019年08月23日 16:48:56   作者:chengziaa123  
這篇文章主要為大家詳細介紹了利用SpringMVC和Ajax實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

個人根據(jù)相關資料實現(xiàn)利用SpringMVC和Ajax實現(xiàn)文件上傳功能:

環(huán)境:

1.JDK1.7

2.maven3.3.9

3.Tomcat7

第一步:

導入相關jar包:

第二步:

配置springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 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">
 
 <context:component-scan base-package="com.lc" />
 
 <!-- 配置視圖解析器 -->
 <bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/page/"></property>
 <property name="suffix" value=".jsp"></property>
 </bean>
 
 
 <bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!--上傳文件的最大大小 -->
 <property name="maxUploadSize" value="17367648787"></property>
 <!-- 上傳文件的編碼 -->
 <property name="defaultEncoding" value="UTF-8"></property>
 </bean>
 
</beans>

第三步:

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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">
 <display-name>fileupload</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <!--Springmvc的控制分發(fā)器 -->
 <servlet>
 <servlet-name>springDispatcherServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc-config.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springDispatcherServlet</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 
</web-app>

第四步:

新建一個Controller類,并實現(xiàn)文件上傳的功能

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<String, String> 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();// 如果目錄不存在,則創(chuàng)建目錄
  }
  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文件夾,并創(chuàng)建實現(xiàn)文件上傳的jsp或者HTML文件(我使用的是jsp):

在index.jsp下寫入相關的ajax的方法,在使用ajax之前必須先導入js庫。

<body>
 <form id="uploadForm" enctype="multipart/form-data" method="post">
 <input type="file" name="file">
 </form>
 <br>
 <input type="button" id="upload" value="上傳">
</body>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
 $(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("上傳失敗");
 
  });
 });
 });
</script>

第六步:

進行測試

上傳文件

上傳成功

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中的強引用,軟引用,弱引用,虛引用的作用介紹

    Java中的強引用,軟引用,弱引用,虛引用的作用介紹

    這篇文章主要介紹了Java中的強引用,軟引用,弱引用,虛引用的作用,下文內容具有一定的知識參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-02-02
  • Java框架Quartz中的Trigger簡析

    Java框架Quartz中的Trigger簡析

    這篇文章主要介紹了Java框架Quartz中的Trigger簡析,所有類型的trigger都有TriggerKey這個屬性,表示trigger的身份;除此之外,trigger還有很多其它的公共屬性,這些屬性,在構建trigger的時候可以通過TriggerBuilder設置,需要的朋友可以參考下
    2023-11-11
  • java導出excel 瀏覽器直接下載或者或以文件形式導出

    java導出excel 瀏覽器直接下載或者或以文件形式導出

    這篇文章主要介紹了java導出excel 瀏覽器直接下載或者或以文件形式導出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • mybatis-plus id主鍵生成的坑

    mybatis-plus id主鍵生成的坑

    這篇文章主要介紹了mybatis-plus id主鍵生成的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • 淺談Springboot整合RocketMQ使用心得

    淺談Springboot整合RocketMQ使用心得

    本篇文章主要介紹了Springboot整合RocketMQ使用心得,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Springboot?application.yml配置文件拆分方式

    Springboot?application.yml配置文件拆分方式

    這篇文章主要介紹了Springboot?application.yml配置文件拆分方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 濫用@PathVariable導致bug原因分析解決

    濫用@PathVariable導致bug原因分析解決

    這篇文章主要為大家介紹了濫用@PathVariable導致bug原因分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Spark?集群執(zhí)行任務失敗的故障處理方法

    Spark?集群執(zhí)行任務失敗的故障處理方法

    這篇文章主要為大家介紹了Spark?集群執(zhí)行任務失敗的故障處理方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 關于SpringCloud整合RabbitMQ的實例

    關于SpringCloud整合RabbitMQ的實例

    這篇文章主要介紹了關于SpringCloud整合RabbitMQ的實例,消息隊列是指利用高效可靠的消息傳遞機制進行與平臺無關的數(shù)據(jù)交流,并基于數(shù)據(jù)通信來進行分布式系統(tǒng)的集成,是在消息的傳輸過程中保存消息的容器,需要的朋友可以參考下
    2023-07-07
  • 深入理解Java中線程間的通信

    深入理解Java中線程間的通信

    一般來講,線程內部有自己私有的線程上下文,互不干擾。但是當我們需要多個線程之間相互協(xié)作的時候,就需要我們掌握Java線程的通信方式。本文將介紹Java線程之間的幾種通信原理,需要的可以參考一下
    2022-11-11

最新評論