免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
java 文件上傳

一、前端頁(yè)面

1、html

 (1)設(shè)置input 的type類型為file,代表 用于文件上傳。

 (2)accept屬性,它規(guī)定能夠通過(guò)文件上傳進(jìn)行提交的文件類型。accept值是 MIME 類型列表,多個(gè)類型之間用逗號(hào)隔開(kāi)

(3)multiple 屬性是 HTML5 中的新屬性。屬性規(guī)定輸入字段可選擇多個(gè)值。多文件上傳

<div >
<input id="addFile" class="form-control" class="filepath" type="file" multiple="multiple" accept="application/msword,application/vnd.ms-works,text/plain,application/pdf,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document"><br>
</div>
2、js
add: function () {
    var file = document.getElementById("addFile").files[0];
    if (file == null) {
        toastr.error('請(qǐng)上傳文件');
        return false;
    }
    // 創(chuàng)建form對(duì)象
    var param = new FormData();
    // 通過(guò)append向form對(duì)象添加數(shù)據(jù)
    param.append('file', file);
    param.append('token', $('#token').val());
    // 上傳需要將對(duì)應(yīng)的文件類型上傳的數(shù)據(jù)庫(kù)
    param.append('fileType', fileType);
    $.ajax({
        cache: false,
        type: "POST",
        url: backbasePath + '/apia/v1/file/uploadFile',
        data: param,
        async: true,
        contentType: false,
        processData: false,
        success: function (data) {
            data = eval("(" + data + ")");
            if ('000000' == data.code) {
                toastr.success(data.msg);
                //上傳成功之后清楚掉之前選擇的文件
                $("#addFile").val("");
                // 上傳成功之后進(jìn)行table的重新加載
                $('#filesList').DataTable().ajax.reload();
            } else if ('900000' == data.code) {
                toastr.error('上傳失?。?);
            } else {
                toastr.error(data.msg);
            }
            $("#upload").modal('hide');
        },
        error: function () {
            toastr.error('上傳失?。?);
            $("#upload").modal('hide');
        }
    });
},
二、后端代碼
    // 上傳文件
    @RequestMapping("/uploadFile")
    public Object upload(HttpServletRequest request, @RequestParam(required = false) MultipartFile file) {
        String result = null;if (null != file && !file.isEmpty()) {
            try {
                // 檢查文件大小
                long fileSize = file.getSize();
                if (fileSize > 1 * 1024 * 1024) {
                    return RequestResponseTool.getJsonMessage(RespCode.repeat, "上傳失敗!上傳的文件大小超出了1M限制!");
                }
                // 檢查文件MIME類型
                String contentType = file.getContentType();
                List<String> types = new ArrayList<String>();
                //擴(kuò)展名 doc dot
                types.add("application/msword");
                //擴(kuò)展名 docx
                types.add("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                //擴(kuò)展名 pdf
                types.add("application/pdf");
                //擴(kuò)展名 txt
                types.add("text/plain");
                //擴(kuò)展名 wps
                types.add("application/vnd.ms-works");
                //擴(kuò)展名 xla xlc xlm xls xlt xlw
                types.add("application/vnd.ms-excel");
                if (!types.contains(contentType)) {
                    return RequestResponseTool.getJsonMessage(RespCode.repeat, "上傳失??!不允許上傳此類型的文件!");
                }
                // 獲取原始文件名
                String originalFilename = file.getOriginalFilename();
                String path = filePath;
                path = path + "/upload/";//定義位絕對(duì)路徑
                File parent = new File(new File(path).getAbsolutePath());
                System.out.println("\tparent=" + parent);
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Map<String, Object> m = new HashMap<String, Object>();
                // 根據(jù)文件名稱進(jìn)行查詢,如果存在則更新否則新增
                Map<String, Object>  fileMap = knowledgeService.getFileByName(originalFilename);
                // 如果能查詢出對(duì)應(yīng)的數(shù)據(jù),則進(jìn)行更新操作
                if(fileMap !=null && fileMap.size() >0){
                    String id =fileMap.get("id").toString();
                    String oldfileName =fileMap.get("file_name").toString();
                    // 找到之前的文件,如果存在則刪除
                    File oldFile = new File( path+"/"+oldfileName);
                    if (oldFile.exists()) {
                        oldFile.delete();
                    }
                    // 保存當(dāng)前的文件
                    file.transferTo(new File(parent, oldfileName));
                    // 更新文件表中的大小
                    m.put("id", id);
                    m.put("file_size", fileSize);
                    result = knowledgeService.update(m);
                }
                // 如果查不到數(shù)據(jù),則進(jìn)行新增操作
                else {
                    // 新文件名稱
                    String filename = UUID.randomUUID().toString();
                    String suffix = "";
                    int beginIndex = originalFilename.lastIndexOf(".");
                    if (beginIndex != -1) {
                        suffix = originalFilename.substring(beginIndex);
                    }
                    // 執(zhí)行保存文件
                    file.transferTo(new File(parent, filename + suffix));
                    // 存放的文件路徑
                    String file_path = "/upload/" + filename + suffix;
                    //id
                    String knowledgeId = IDCode.knowledgeId + IDTool.getWebUserId() + "";
                    //文件表Id
                    String file_id = IDCode.fileId + IDTool.getWebUserId() + "";
                    //文件邏輯名稱(和路徑中的名稱保持一致)
                    String file_name = filename + suffix;
                    //  知識(shí)資源表中的主鍵
                    m.put("id", knowledgeId);// 文件id
                    m.put("file_id", file_id);// 創(chuàng)建時(shí)間
                    m.put("create_time", dateFormat.format(date));
                    m.put("file_name", file_name);
                    m.put("file_real_name", originalFilename);
                    m.put("file_path", file_path);
                    m.put("file_size", fileSize);
            // 執(zhí)行新增操作 result
= knowledgeService.add(m); } return result; } catch (Exception ex) { ex.printStackTrace(); } } return result; }

 



本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JAVA中比較兩個(gè)文件夾不同的方法
Android 中文件類型與MIME的匹配表
開(kāi)發(fā)|Springboot文件上傳與下載
學(xué)習(xí)簡(jiǎn)單圖片的計(jì)數(shù)器的制作
VBA: 獲取電腦當(dāng)前默認(rèn)打印機(jī)的名稱
文件下載的其中之一種
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服