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);
    }
}

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

posted @ 2015-12-17 16:49  huanshare  阅读(369)  评论(0编辑  收藏  举报