zoukankan      html  css  js  c++  java
  • Huploadify V2.1+ SpringMVC上传文件的实现

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>学生成绩信息</title>
    <link rel="stylesheet" type="text/css"
    href="<%=basePath%>jquery/Huploadify-V2.1.2/Huploadify.css">
    <script type="text/javascript"
    src="<%=basePath%>jquery/Huploadify-V2.1.2/jquery.js"></script>
    <script type="text/javascript"
    src="<%=basePath%>jquery/Huploadify-V2.1.2/jquery.Huploadify.js"></script>
    </head>
    <script type="text/javascript">
    $(function(){
    var up = $('#upload').Huploadify({
    auto:false,
    fileTypeExts:'*.jpg;*.png;*.exe;*.mp3;*.mp4;*.zip;*.doc;*.docx;*.ppt;*.pptx;*.xls;*.xlsx;*.pdf',
    multi:true,
    fileSizeLimit:99999999,
    breakPoints:true,
    saveInfoLocal:true,
    showUploadedPercent:true,//是否实时显示上传的百分比,如20%
    showUploadedSize:true,
    removeTimeout:9999999,
    uploader:'<%=basePath%>'+ 'fileUpload',
    onUploadStart:function(){
    //up.settings('formData', {aaaaa:'1111111',bb:'2222'});
    up.Huploadify('settings','formData', {aaaaa:'1111111',bb:'2222'});
    },
    onUploadSuccess:function(file){
    //alert('上传成功');
    },
    onUploadComplete:function(){
    //alert('上传完成');
    },
    /*getUploadedSize:function(file){
    var data = {
    data : {
    fileName : file.name,
    lastModifiedDate : file.lastModifiedDate.getTime()
    }
    };
    var url = 'http://localhost/uploadfile/';
    var uploadedSize = 0;
    $.ajax({
    url : url,
    data : data,
    async : false,
    type : 'POST',
    success : function(returnData){
    returnData = JSON.parse(returnData);
    uploadedSize = returnData.uploadedSize;
    }
    });
    return uploadedSize;
    } */
    });

    $('#btn1').click(function(){
    up.stop();
    });
    $('#btn2').click(function(){
    up.upload('*');
    });
    $('#btn3').click(function(){
    up.cancel('*');
    });
    $('#btn4').click(function(){
    up.disable();
    });
    $('#btn5').click(function(){
    up.ennable();
    });


    });
    </script>
    <body>
    <div id="upload"></div>
    <button id="btn1">stop</button>
    <button id="btn2">upload</button>
    <button id="btn3">cancel</button>
    <button id="btn4">disable</button>
    <button id="btn5">ennable</button>
    </body>
    </html>

    /**
    * 查询学生信息列表
    *
    * @param name
    * @return
    * @throws NamingException
    * @throws SQLException
    */
    @ResponseBody
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    public String fileUpload(@RequestParam Map<String, Object> map, HttpServletRequest request,
    HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file) {
    try {
    String fileName = request.getParameter("fileName");
    byte[] bytes = file.getBytes();
    System.out.println(file.getOriginalFilename());
    String uploadDir = request.getRealPath("/") + "upload";
    File dirPath = new File(uploadDir);
    if (!dirPath.exists()) {
    dirPath.mkdirs();
    }
    String sep = System.getProperty("file.separator");
    File uploadedFile = new File(uploadDir + sep + fileName);
    FileCopyUtils.copy(bytes, uploadedFile);
    } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    }
    return "ok";
    }

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    p:defaultEncoding="UTF-8" />
    </beans>

  • 相关阅读:
    Java中swing常用控件背景设置透明的方法
    SpringMVC工作原理
    关于spring框架工作原理
    Java-JFrame窗体美化方式
    Java swing GUI窗口美化
    Java-Swing是什么?
    SSM+Redis+Layui前端框架实现验证码的发送
    SSM+Redis+Layui实现注册功能
    SSM+Layui实现模拟登录功能
    SSM+layui分页(了解98%)
  • 原文地址:https://www.cnblogs.com/wshsdlau/p/5632459.html
Copyright © 2011-2022 走看看