SpringMVC上传文件、下载文件

<form id="uploadForm" enctype="multipart/form-data">
    <span style="margin-right: 65px;">词典名称:</span>
    <select class="easyui-combobox" style="width: 180px;" id="batchAddPanelDictConditionSelect" name="dictId">
    </select>    
    <div style="margin-bottom: 20px">
        <input class="easyui-filebox" label="Excel文件:" name="excelFile" id="excelFileId" labelPosition="left"
            data-options="prompt:'Choose a excel file...'" style="width: 100%">
    </div>
</form>
function commit() {

    var dictId = $('#batchAddPanelDictConditionSelect').combobox('getValue');
    if (dictId == '') {
        showWarningMessage('请选择词典!');
        return;
    }
    var excelFile = $('#excelFileId').textbox('getValue');
    if (excelFile == '') {
        showWarningMessage('请选择需要上传的excel!');
        return;
    }
    var regex = /^.+?(\.xlsx)|(\.xls)$/;
    if (!regex.test(excelFile)) {
        showWarningMessage('上传的文件必须是excel!');
        return;
    }

    var formData = new FormData($('#uploadForm')[0]);

    var options = {
        url: 'entry/batchAddEntries',
        type: 'POST',
        dataType: 'json',
        data: formData,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        beforeSend: function () {
            console.log("正在进行,请稍候");
        },
        success: function (rs) {
        },
        error: function (rs) {
        }
    };
    $.ajax(options);
};
@RestController
@RequestMapping(value = { "/entry" })
public class EntryController  {
    private static final Logger LOGGER = LoggerFactory.getLogger(EntryController.class);
    /**
     * excel文件后缀形式1
     */
    private static final String EXCEL_SUFFIX_01 = ".xls";
    /**
     * excel文件后缀形式2
     */
    private static final String EXCEL_SUFFIX_02 = ".xlsx";

    @RequestMapping(value = { "/batchAddEntries" }, method = { RequestMethod.POST })
        public Result<Void> batchAddEntries(@RequestParam("dictId") Integer dictId,
                @RequestParam("excelFile") MultipartFile excelFile) {
            LOGGER.debug("start to batchAddEntries. dictId:{},excelFile:{}", dictId, excelFile);
            checkNotNull(dictId, "dictId is null");
            checkNotNull(excelFile, "excelFile is null");
            String lowcaseName = excelFile.getName().toLowerCase();
            checkArgument(lowcaseName.endsWith(EXCEL_SUFFIX_01) || lowcaseName.endsWith(EXCEL_SUFFIX_02),
                    "upload file is not a excel file");

            // TODO upload excel
            return null;
        }


    @RequestMapping(value = { "/download" }, method = { RequestMethod.GET })
    public ResponseEntity<byte[]> download(HttpServletRequest request,
            @RequestParam("fileName") String fileName) throws Exception {
        // 下载文件路径
        String path = request.getServletContext().getRealPath("/static/template/");
        File file = new File(path + File.separator + fileName);
        HttpHeaders headers = new HttpHeaders();
        // 通知浏览器以attachment(下载方式)打开图片
        headers.setContentDispositionFormData("attachment", fileName);
        // application/octet-stream : 二进制流数据(最常见的文件下载)。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }
    
}

  

 参考地址: http://blog.csdn.net/qian_ch/article/details/69258465

猜你喜欢

转载自kanpiaoxue.iteye.com/blog/2410061
今日推荐