jeect 上传文件

本文档介绍了在JeecgBoot中实现文件上传的步骤,包括在附件实体类中定义关键属性,如路径、文件名和创建时间,并在jsp页面中使用js和upload标签进行文件上传操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.附件实体类中要有这3个属性

/**资料 标 外键*/
private CaseDataEntity caseDataEntity;
/**路径*/
private java.lang.String realpath;
/**文件名称*/
private java.lang.String attachmenttitle;
/**创建时间*/
private java.lang.String createdate;
/**后缀名称*/
private java.lang.String extend;


2. jsp  ,加入js,修改表单(回调),upload标签,回调后的表单

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>案件信息</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
  
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<script type="text/javascript">
  $(function(){
    //查看模式情况下,删除和上传附件功能禁止使用
	if(location.href.indexOf("load=detail")!=-1){
		$(".jeecgDetail").hide();
	}
   });

  	function uploadFile(data){
  		$("#financeId").val(data.obj.id);
  		$("#fileType").val(data.obj.modifyUser);
  		if($(".uploadify-queue-item").length>0){
  			upload();
  		}else{
  			frameElement.api.opener.reloadTable();
  			frameElement.api.close();
  		}
  	}
  	
  	function close(){
  		frameElement.api.close();
  	}
  </script>
<!-- 弹出页面窗口大小控制 -->
<style type="text/css">
#formobj {
	height: 65%;
	min-height: 300px;
	overflow-y: auto;
	overflow-x: auto;
	min-width: 600px;
}
</style>
 </head>
 <body >
 <t:formvalid formid="formobj" dialog="true" usePlugin="password" layout="div" callback="@Override uploadFile" action="zlwjController.do?show">
	
		<input id="id" name="id" type="hidden" value="${CaseInfosEntity.id }">
		
		<fieldset class="step">
		
		    <t:dictSelect id="fType" field="modifyUser" typeGroupCode="filesType" title="上传类型" datatype="*" ></t:dictSelect>
		   
			<div class="form" id="filediv"></div>
			<div class="form jeecgDetail">
			<t:upload name="fiels" id="file_upload" extend="*.doc;*.docx;*.txt;*.ppt;*.xls;*.xlsx;*.html;*.htm;*.pdf;" buttonText="添加文件" formId="uploadForm" uploader="zlwjController.do?saveFiles">
			</t:upload>
	    </fieldset>
  </t:formvalid>
  
  <form action="" id ="uploadForm"> <input type="hidden" value="${caseInfosPage.id}" id="financeId" name="financeId" /> 
  		<input type="hidden"  id="fileType" name="fileType" />
  </form>
  
 </body>


3.修改controller的save()加入  j.setObj(实体类);再加上saveFiles方法;
/**
	 * 添加案件信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(CaseInfosEntity caseInfos, HttpServletRequest request,ModelMap map) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(caseInfos.getId())) {
			message = "案件信息更新成功";
			CaseInfosEntity t = caseInfosService.get(CaseInfosEntity.class, caseInfos.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(caseInfos, t);
				caseInfosService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "案件信息更新失败";
			}
		} else {
		  
			//add修改人,修改时间,有效标识
			//caseInfos.setModifyUser(ResourceUtil.getSessionUserName().getRealName());
			caseInfos.setModifyTime(new Date());
			caseInfos.setYxbz(0);
			
			
		    // 级联保存 
			message = "案件信息添加成功";
			caseInfosService.save(caseInfos);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}

		
		j.setObj(caseInfos);
		
		return j;
	}
	
	/**
	 * 保存文件
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "saveFiles", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson saveFiles(HttpServletRequest request, HttpServletResponse response, ZlwjEntity zlwj) {
		AjaxJson j = new AjaxJson();
		Map<String, Object> attributes = new HashMap<String, Object>();
		String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID
		
		String financeId = oConvertUtils.getString(request.getParameter("financeId"));//案件ID
		
		String filesType = oConvertUtils.getString(request.getParameter("fileType"));//类别
		
		if (StringUtil.isNotEmpty(fileKey)) {
			zlwj.setId(fileKey);
			zlwj = systemService.getEntity(ZlwjEntity.class, fileKey);
		}
		CaseInfosEntity caseinfo = systemService.getEntity(CaseInfosEntity.class, financeId);
		zlwj.setCaseInfosEntity(caseinfo);
		
		//add修改人,修改时间,有效标识,文件类别为申请书
		zlwj.setModifyUser(ResourceUtil.getSessionUserName().getRealName());
		zlwj.setModifyTime(new Date());
		zlwj.setYxbz(0);
		zlwj.setLb(filesType);
		
		UploadFile uploadFile = new UploadFile(request, zlwj);
		uploadFile.setCusPath("files");
		uploadFile.setSwfpath("swfpath");
		uploadFile.setByteField(null);//不存二进制内容
		zlwj = systemService.uploadFile(uploadFile);
		attributes.put("fileKey", zlwj.getId());
		attributes.put("viewhref", "commonController.do?objfileList&fileKey=" + zlwj.getId());
		attributes.put("delurl", "commonController.do?delObjFile&fileKey=" + zlwj.getId());
		
		j.setMsg("文件添加成功");
		j.setAttributes(attributes);

		return j;
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值