java使用ajax完成上傳文件
本文實(shí)例為大家分享了java使用ajax完成上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
使用ajax上傳文件最重要的就是實(shí)例化FormData來進(jìn)行上傳文件:
1、html代碼:
<div class="kuang"> ?? ?<p>文件上傳</p> ?? ?<div class="san3"> ?? ??? ?<input type="file" name="fujianID" id="fujianID" onchange="UpdateFile()"/> ?? ??? ?<input type = "hidden" name="fujianid" id="file2" /> ?? ?</div> </div>
2、ajax請求代碼:
<script type="text/javascript"> //上傳文件 function UpdateFile(){ ?? ?var file_fujian = $("input[name='fujianID']").val();//用戶文件內(nèi)容(文件) ?? ?// 判斷文件是否為空? ?? ?if ( file_fujian== "") { ?? ??? ?layer.msg("青選擇上傳的附件",{icon:1,time:1000}); ?? ??? ?return false; ?? ?} ?? ?//判斷文件類型,根據(jù)自己需要來做判斷 ?? ?/*var fujianID = ?file_fujian.substring( file_fujian.lastIndexOf(".") + 1).toLowerCase(); ?? ?if(fujianID != "xls" && fujianID !="xlsx"&&fujianID !="docx"&&fujianID !="doc"&&fujianID !="txt"){ ? ? ? ?? ?layer.msg("請選擇Execl文或者word文件或者txt文件格式!",{icon:1,time:2000}); ?? ??? ?return false; ?? ?}*/ ?? ?//判斷文件大小 ?? ?var size1 = $("input[name='fujianID']")[0].files[0].size; ?? ?if (size1>104857600) { ?? ??? ?layer.msg("上傳的附件不能大于100M",{icon:1,time:2000}); ?? ??? ?return false;?? ??? ??? ??? ? ?? ?} ?? ? ?? ?boo1 = true; ?? ?var type = "file"; ?? ?var formData = new FormData();//這里需要實(shí)例化一個FormData來進(jìn)行文件上傳 ?? ?formData.append(type,$("#fujianID")[0].files[0]); ?? ?$.ajax({ ? ? ? ? type:'POST', ? ? ? ? url:'<%=request.getContextPath()%>/login/fujian_file', ?? ??? ?data:formData, ?? ??? ?async:false, ?? ??? ?processData:false, ?? ??? ?contentType:false, ?? ??? ?success:function(data){ ?? ??? ??? ?if (data=="error") { ?? ??? ??? ??? ?layer.msg("附件添加失敗",{icon:1,time:2000}); ?? ??? ??? ?}else{ ?? ??? ??? ??? ?$("#file2").val(data.list); ?? ??? ??? ??? ?layer.msg("附件已添加",{icon:1,time:2000}); ?? ??? ??? ?} ?? ??? ?} ?? ?}); }
3、Java后臺數(shù)據(jù)處理代碼:
?/** ?? ? * 上傳文件 ?? ? * @param file ?? ? * @param model ?? ? * @param request ?? ? * @return ?? ? */ ?? ?@RequestMapping(value="/fujian_file") ? ? @ResponseBody ? ? public Map<String, Object> uploadToUser(@RequestParam("file") MultipartFile file, Model model,HttpServletRequest request) { ?? ??? ? ?? ??? ?String fileName = file.getOriginalFilename(); ?? ??? ?if (fileName.indexOf("\\") != -1) { ?? ??? ? ? ?fileName = fileName.substring(fileName.lastIndexOf("\\")); ?? ??? ?} ?? ??? ? ?? ??? ?//String filePath = request.getContextPath()+ File.separator+"WebRoot"+ File.separator+"updatefile"+ File.separator; ?? ??? ?// 獲取項(xiàng)目名稱,文件上傳位置 ?? ??? ?String filePath = ?request.getSession().getServletContext().getRealPath("/static/updatefile/files"); ?? ??? ?File f = new File(filePath); ?? ??? ?if (!f.exists()) { ?? ??? ? ? ?f.mkdirs();// 不存在路徑則進(jìn)行創(chuàng)建 ?? ??? ?} ?? ??? ?FileOutputStream out = null; ?? ??? ?try { ?? ??? ? ? ?// 重新自定義文件的名稱 ?? ??? ? ? ?Date date = new Date(); ?? ??? ? ? ?SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); ?? ??? ? ? ?String d = sdf.format(date);// 時間 ?? ??? ? ? ?//拼接文件路徑和文件名 ?? ??? ? ? ?filePath = filePath +File.separator+ d + fileName; ?? ??? ? ? ?out = new FileOutputStream(filePath); ?? ??? ? ? ?out.write(file.getBytes()); ?? ??? ? ? ?out.flush();//清空緩沖區(qū)的數(shù)據(jù)流 ?? ??? ? ? ?out.close();//關(guān)閉流 ?? ??? ??? ?} catch (Exception e) { ?? ??? ??? ??? ?//System.out.println(e); //打印拋出的異常 ?? ??? ??? ?} ?? ??? ?Map<String,Object> map = new HashMap<String,Object>(); ?? ??? ?map.put("list",filePath ); ?? ??? ?return map; // 返回文件地址 ?? ?}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)excel生成并且通過郵件發(fā)送的步驟詳解
實(shí)際開發(fā)中,特別是在B端產(chǎn)品的開發(fā)中,我們經(jīng)常會遇到導(dǎo)出excel的功能,更進(jìn)階一點(diǎn)的需要我們定期生成統(tǒng)計(jì)報(bào)表,然后通過郵箱發(fā)送給指定的人員,?今天要帶大家來實(shí)現(xiàn)的就是excel生成并通過郵件發(fā)送,需要的朋友可以參考下2023-10-10使用SpringMVC的@Validated注解驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了使用SpringMVC的@Validated注解驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Spring?Boot中application配置文件的生效順序及應(yīng)用范圍
Spring?Boot的一個重要特性就是它的自動配置,這一特性在很大程度上依賴于名稱為application的配置文件,本文將詳細(xì)介紹在Spring?Boot中,這些配置文件的加載順序以及每份文件的應(yīng)用范圍,需要的朋友可以參考下2024-03-03