jquery+springMVC實現檔案上傳,jqueryspringmvc

來源:互聯網
上載者:User

jquery+springMVC實現檔案上傳,jqueryspringmvc

此文章是基於

  1. 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

  2. web工程常用路徑的擷取方法

  3. 熱門檔案(夾)處理方法工具類

 

一. 準備工作

  1. 點擊此下載相關檔案,並把檔案放到 ims 工程對應的檔案夾下

 

二. jar包介紹

  1. commons-fileupload-1.3.1.jar

 

三. 相關程式碼

  1. applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">         <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >        <property name="maxUploadSize">            <value>52428800</value>        </property>        <property name="maxInMemorySize">            <value>2048</value>        </property>    </bean>    </beans>
View Code

  

  2. TestController.java

package com.ims.web.controller;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSON;import com.ims.common.FileUtil;@Controller@RequestMapping("test")public class TestController extends BaseController{        @RequestMapping("view")    public ModelAndView test(){        ModelAndView view = new ModelAndView("test.jsp");        return view;    }        @RequestMapping("fileUpload")    public void fileUpload(MultipartHttpServletRequest multipartRequest){        Map<String, Object> result = new HashMap<String, Object>();        try{            MultipartFile uploadFile = multipartRequest.getFile("uploadFile");            String fileName = uploadFile.getOriginalFilename();            String uploadPath = System.getProperty("webapp.root")+"uploadFile\\";            FileUtil.saveFileFromInputStream(uploadFile.getInputStream(),                     uploadPath+fileName);        } catch (IOException e) {            result.put("status", STATUS_ERROR);            result.put("message", "檔案上傳失敗");        }            ajax(JSON.toJSONString(result),"text/html");    }    }
View Code

  

  3. test.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html> <head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>測試</title>    <%@ include file="/common/basePath.jsp"%> </head> <body> ~~~~~~~~~~~~~~~~~~~~~~檔案上傳~~~~~~~~~~~~~~~~~~~~~~~~ <br><br>   檔案選擇:<input type="file" id="uploadFile" style="width: 350px;"/> <br><br> <button type="button" onclick="upload();">上傳</button> <br><br><br> <script type="text/javascript" src="content/js/jquery/jquery-1.8.1.min.js"></script> <script type="text/javascript" src="content/js/jquery-plugin/fileUpload/jquery.ajaxFileUpload.js"></script> <script type="text/javascript">        function upload(){        $.ajaxFileUpload({              url:rootPath+"/test/fileUpload.do",            secureuri:false,            fileElementId: ['uploadFile'],             dataType: 'json',            success: function (data){                                          },            error: function(data){            }        });    } </script> </body></html>
View Code

 

四. 測試

  訪問:http://localhost:8090/ims/test/view.do

  選擇一檔案,點上傳,則在  %工程發布目錄%\WebContent\uploadFile  下可看到上傳的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.