SSH + EasyUI文件上传

前端

<div id="dlg3" class="easyui-dialog" resizable="true" maximizable="true" closed="true" modal="true"
	style="width:650px;height:400px;padding:10px 20px" buttons="#dlg-buttons3">
	<form id="saveForm3" method="post" enctype="multipart/form-data">
		<div class="form-title">语音设置</div>
		<table cellpadding="5">
		
			<%-- <tr>
				<td>请选择城市公司:</td>
				<td>
					<s:select id = "companyId" name="companyId"  list="#companyList" style="width:180px;" data-options="editable:true"
							headerKey="" headerValue="" cssClass="easyui-combobox" listKey="id" listValue="name" ></s:select>
				</td>
			</tr>
			<tr>
				<td>请选择语音类型:</td>
				<td>
					<s:select id = "voiceType" name="voiceType"  list="#dictionariesItemList" style="width:180px;" data-options="editable:true"
							headerKey="" headerValue="" cssClass="easyui-combobox" listKey="id" listValue="itemName" ></s:select>
				</td>
			</tr> --%>
			<tr>
				<td>请输入语音名字:</td>
				<td>
					<input id = "voiceName" name="voiceName" class="easyui-textbox"   style="width:180px">
				</td>
			</tr>
			<tr>
				<td>请输入语音内容:</td>
				<td><input id = "content" name="content" class="easyui-textbox" data-options="multiline:true" 
						value="请输入语音内容(中文翻译)..." style="width:300px;height:100px"></td>
			</tr>
			<tr>
				<td>请选择语音文件:</td>
				<td><input id = "holidayVoice" name="holidayVoice" class="easyui-filebox"  data-options="prompt:'选择附件...',buttonText:'选择附件'" style="width:200px"></td>
			</tr>
			
		</table>
	</form>
</div>

后端

/**
	 * 节假日管理
	 * @author lxl  
	 * @return
	 * @date 2019年6月10日  
	 * @version 1.0
	 */
	public String edit3(){
		//上传文件存储路径
		//获得ActionContest
        ActionContext ac = ActionContext.getContext();  
        //通过ActionContext 可获得Session 和  servletContext(applicationContext) ...
	    ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);  
	    //获得项目路径
	    String path = sc.getRealPath("/");
		
		User loginUser = getLoginUser();
		Department company = departmentService.getCompany(loginUser.getDepartment());
		PhoneIVR ivr = null;
		
		if(holidayVoice != null){
			//通过id获取字典对象
			DictionariesItem dictionariesItem = dictionariesItemService.getById(PhoneIVRUtil.PHONE_IVR_HOLIDAY);
			//根据公司和语音类型获取ivr语音
			/*ivr = phoneIVRService.getIVR(dictionariesItem, company);
			if(ivr!=null){
				//判断ivr里的VoivePath是否有值,若有则删除
				File existIVR= new File(ivr.getVoicePath());
				if(existIVR != null ){
					existIVR.delete();
				}
			}*/
			//上传文件到服务器
			//获得唯一标识  UUID + 文件后缀(对文件重命名)
			String uuidName = UUID.randomUUID().toString()+holidayVoiceFileName.substring(holidayVoiceFileName.lastIndexOf("."));
			//完整的上传路径
			String voicePath = path+"IVRvoice/"+uuidName;
			boolean uploadFile = uploadFile(holidayVoice,holidayVoiceFileName,voicePath);
			if(uploadFile){
				if(ivr==null){
					ivr = new PhoneIVR();
				}
				ivr.setCompany(company);
				ivr.setVoiceType(dictionariesItem);
				ivr.setVoicePath(voicePath);
				ivr.setVoiceName(model.getVoiceName());
				ivr.setContent(model.getContent());
				phoneIVRService.save(ivr);
				
				FtpUtil ftpUtil = new FtpUtil();
				ftpUtil.doUploadSet(voicePath, uuidName);
			}else{
				jsonMsg.setMsg("个性化语音设置失败!");
		        jsonMsg.setSuccess(false);
			}
		}
		
		jsonMsg.setMsg("个性化语音设置成功!");
        jsonMsg.setSuccess(true);
		return JSONMSG;
		
	}
	
	public String delectMusic(){
		try {
			phoneIVRService.delete(model.getId());
			jsonMsg.setMsg("删除成功!");
	        jsonMsg.setSuccess(true);
		} catch (Exception e) {
			jsonMsg.setMsg("删除失败!");
	        jsonMsg.setSuccess(false);
		}
		return JSONMSG;
	}
	
	/*
	 * 上传附件
	 */
	private boolean uploadFile(File file,String fileName,String voicePath) {
        // 构建目标文件  
		//根据存储路径,创建File
        File fileCopy = new File(voicePath);  
        InputStream in = null;  
        OutputStream out = null;  
        try {  
            // 目标文件不存在就创建  
            if (!(fileCopy.exists())) {  
                fileCopy.createNewFile();  
            }  
            // 源文件创建输入流  
            in = new FileInputStream(file);  
            // 目标文件创建输出流  
            out = new FileOutputStream(fileCopy, false);  
            // 创建字节数组  
            byte[] temp = new byte[1024];  
            int length = 0;  
            // 源文件读取一部分内容  
            while ((length = in.read(temp)) != -1) {  
                // 目标文件写入一部分内容  
                out.write(temp, 0, length);  
            }  
//            model.setAttachName(importDataFileName);
//            String attachPath = "attachment/"+fileCopy.getName();
//            model.setAttachPath(attachPath);
        } catch (Exception e) {  
            e.printStackTrace();
            return false;
        } finally {  
            try {  
                // 关闭文件输入输出流  
                in.close();  
                out.close();
            } catch (Exception e) {  
                e.printStackTrace();  
            } 
        }
		return true;  
	}
	~~~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值