zoukankan      html  css  js  c++  java
  • jquery+springMVC实现文件上传

    此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

    一. 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. fileUpload.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=" 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/file!upload.do",
                secureuri:false,
                fileElementId: ['uploadFile'], 
                dataType: 'json',
                success: function (data){                              
                },
                error: function(data){
                }
            });
        }
    
     </script>
     </body>
    </html>
    View Code


    三. 测试

      访问:http://localhost:8080/ims/test/fileUpload.do

      选择一文件,点上传,则在  %工程发布目录%WebContentuploadFile  下可看到上传的文件

  • 相关阅读:
    C++ <cstring> 里的一些常用函数
    Hadoop_第一次作业
    线性回归理解和应用例子
    条款28 :避免返回handles指向对象内部成分
    条款25 :尽可能延后变量定义式的出现时间
    条款21 :必须返回对象时,别妄想返回其reference
    条款16:成对使用new和delete时要采用相同的形式
    条款22 :将成员变量声明为private
    条款13:以对象管理资源
    条款12:复制对象时勿忘其每一个成分
  • 原文地址:https://www.cnblogs.com/Mr-kevin/p/6566263.html
Copyright © 2011-2022 走看看