zoukankan      html  css  js  c++  java
  • spring mvc 中Uploadify插件的使用

    具体过程不写了,直接上代码

    jsp代码

    $("#uplodefile").uploadify({
                    'swf': '/statics/uploadify/uploadify.swf',
                    'uploader': '/contacts/method11',
                    'buttonText': '选择文件',
                    'height': 20,
                    'width': 80,
                    'fileTypeDesc': 'Excel 工作表',
                    'fileTypeExts': '*.xls;*.xlsx',
                    ////'formData': { 'Action': 'UploadTopUpRecordsList' },
                    ////选择文件后自动上传
                    'auto': false,
                    ////设置为true将允许多文件上传
                    'multi': false,
                    'sizeLimit': '2048000', //最大允许的文件大小为2M
                    'displayData': 'speed', //进度条的显示方式
                    //'queueID': 'fileQueue',
                    'onUploadSuccess': funComplete //完成上传任务
                });
    <table style="margin: 0 auto;">
            <tr>
                <td style="position: relative;">
                    <input id="uplodefile" name="uplodefile" class="uploadify" type="file" />
                </td>
                <td>
                    <a href="javascript:$('#uplodefile').uploadify('upload')" class="easyui-linkbutton" >上传</a>&nbsp;&nbsp;
                    <a href="javascript:$('#uplodefile').uploadify('cancel')" class="easyui-linkbutton" >取消上传</a>
                </td>
            </tr>
        </table>
    package com.huanshare.service;
    
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    /**
     * Created by huan.liu on 2015/12/17.
     */
    @SuppressWarnings("serial")
    public class Upload extends HttpServlet {
        @SuppressWarnings("unchecked")
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile("Filedata");
            String fileName = multipartFile.getOriginalFilename();
            byte[] bytes = multipartFile.getBytes();
            String path=request.getSession().getServletContext().getRealPath("/");
            path = path + "/uploads/";
           // String savePath = "/servlet/Upload";
            File f1 = new File(path);
            System.out.println(path);
            if (!f1.exists()) {
                f1.mkdirs();
            }
    
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
            File f2 = new File(path+newFileName);
            FileCopyUtils.copy(bytes, f2);
        }
    }

    只是一个简单的例子,自己以后学习使用

  • 相关阅读:
    C# WinForm编程TabControl控件的标签TabPage怎么做成图片
    javascript 用面向对象自写前端验证工具
    javascript 用面向对象自写stringbuffer工具
    java socket 深入学习tomcat 自写动态服务器 tomcat
    java socket 自写静态服务器 apache
    java socket 实现多个客户端通过服务器一对一聊天并实现文件传输
    java socket 实现多个客户端向服务器上传文件
    java socket 实现多个一对一聊天
    java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址
    java 读取文件,内容方置Person 并写到另外地址
  • 原文地址:https://www.cnblogs.com/myhappylife/p/5054622.html
Copyright © 2011-2022 走看看