package com.togest.emis.modules.g6c.web;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.togest.emis.framework.orm.Page;
import com.togest.emis.framework.orm.PropertyFilter;
import com.togest.emis.framework.utils.ConvertUtils;
import com.togest.emis.framework.utils.JSONParserUtils;
import com.togest.emis.framework.utils.RequestParamUtils;
import com.togest.emis.framework.utils.StringHelper;
import com.togest.emis.modules.g6c.entity.Download;
import com.togest.emis.modules.g6c.service.Downloadservice;
import com.togest.emis.modules.system.cache.SystemCache;
import com.togest.emis.modules.system.config.SystemConstants;
/**
*/
@Controller
@RequestMapping(value = "/6c/system/download")
public class DownloadController {
@Autowired
private Downloadservice service;
@RequestMapping(method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request, String id) {
model.addAttribute("psn_id", id);
model.addAttribute(SystemConstants.NAV_PATH,
SystemCache.getPath(id, RequestParamUtils.getUserID(request)));
return "/6c/download";
}
@RequestMapping(value = "getPage", method = RequestMethod.POST)
public void getPage(HttpServletRequest request,
HttpServletResponse response, String name, String address ,Model model) {
Map map = new HashMap();
try {
List filters = PropertyFilter
.buildFromHttpRequest(request);
Page page = RequestParamUtils
.getPage(request, new Page());
if (!page.isOrderBySetted()) {
page.setOrderBy(SystemConstants.OrderByField);
page.setOrder(Page.ASC);
page = service.findPage(page, filters);
}
map.put(SystemConstants.EASYTABLE_PAGE_TOTAL_INDEX,
page.getTotalCount());
map.put(SystemConstants.EASYTABLE_PAGE_RESULT_INDEX,
page.getResult());
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public void save(@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, Model model,HttpServletResponse response, Download d,Map map) {
String path = request.getSession().getServletContext()
.getRealPath("download");
String fileName = request.getParameter("name");
String fileContent = file.getOriginalFilename();
String fileType = fileContent.substring(fileContent.lastIndexOf("."));
String fileAll = fileName+fileType;
String address = path+"\"+fileAll;
String file_path = address.substring(address.lastIndexOf("download"));
String fileUrl = request.getContextPath() + "/download/"
+ fileAll;
// //String fileName = new Date().getTime()+".jpg";
File targetFile = new File(path, fileAll);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
map.put("fileAll", fileAll);
model.addAttribute("address", address);
model.addAttribute("path", path);
model.addAttribute("fileAll", fileAll);
model.addAttribute("fileUrl",fileUrl);
model.addAttribute("fileName", fileName);
System.out.println("+++++++++++++++"+ fileAll);
if (StringHelper.isBlank(d.getId())) {
d.setId(null);
}
//d.setFile(fileAll);
d.setAddress(file_path);
service.save(d);
map = new HashMap<String, Object>();
map.put("msg", "新增成功");
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "edit", method = RequestMethod.GET)
public void edit(HttpServletRequest request, HttpServletResponse response) {
String id = RequestParamUtils.getStringParamter(request, "id");
if (id != null) {
Download d = service.get(id);
try {
Map<String, Object> map = ConvertUtils.objectToMap(d,
new String[] { "id", "sort", "name", "address" });
JSONObject json = JSONObject.fromObject(map);
JSONParserUtils.write(response, json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public void update(HttpServletResponse response, Download d) {
service.save(d);
Map map = new HashMap();
map.put("msg", "修改成功");
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "delete", method = RequestMethod.POST)
public void delete(HttpServletResponse response, String id) {
Map map = new HashMap();
String[] ids = id.split(",");
service.delete(id);
map.put("msg", "删除成功");
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "find", method = RequestMethod.POST)
public String find(Model model, HttpServletRequest request) {
List dlist = service.findAll();
// for(Download d :dlist){
// String dname =d.getName();
// String daddress = d.getAddress();
// request.setAttribute("dname", dname);
// request.setAttribute("daddress", daddress);
// }
model.addAttribute("dlist", dlist);
// ActionContext.getContext().getSession().put("dlist", dlist);
// ActionContext.getContext().getValueStack().set("dlist", dlist);
// request.setAttribute("dlist", dlist);
return "/system/passport/login";
}
}
这是控制器的代码
package com.togest.emis.modules.g6c.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.togest.emis.framework.orm.Page;
import com.togest.emis.framework.orm.PropertyFilter;
import com.togest.emis.modules.g6c.dao.DownloadDao;
import com.togest.emis.modules.g6c.entity.Download;
import com.togest.emis.modules.system.entity.P_Dictionary;
@Service
public class Downloadservice {
@Autowired
private DownloadDao dao;
/**
* 根据主键ID删除实体
*
* @param id
*/
public void delete(String id) {
dao.delete(id);
}
/**
* 保存实体
*
* @param entity
*/
public void save(Download entity) {
dao.save(entity);
}
/**
* 根据主键ID获取实体
*
* @param id
* @return
*/
public Download get(String id) {
return dao.get(id);
}
/**
* 查询所有
*/
public List<Download> findAll(){
String hql ="FROM Download";
List<Download> list = dao.find(hql);
//for(Download d : list){
//System.out.println("+++++++++++++"+d.getName());
//}
return list;
}
public Page<Download> findPage(final Page<Download> page,
final List<PropertyFilter> filters) {
return dao.findPage(page, filters);
}
}
业务层代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<table id="dg" title="${navpath}">
</table>
<div id="tb"
style="height: auto; color: #676767; padding-top: 6px; font-size: 14px;">
<!-- 新增 -->
<c:if test="${lbs:hasPermission(user_id,'DOWNLOADADD')==true}">
<a class="easyui-linkbutton"
data-options="iconCls:'icon-add',plain:true"
onclick="javascript:$('#dg').edatagrid('create')">新增</a>
</c:if>
<!-- 编辑 -->
<c:if test="${lbs:hasPermission(user_id,'DOWNLOADEDIT')==true}">
<a class="easyui-linkbutton"
data-options="iconCls:'icon-edit',plain:true"
onClick="javascript: $('#dg').edatagrid('editlist');">编辑</a>
</c:if>
<!-- 删除 -->
<c:if test="${lbs:hasPermission(user_id,'DOWNLOADDEL')==true}">
<a class="easyui-linkbutton"
data-options="iconCls:'icon-remove',plain:true"
onClick="javascript:$('#dg').edatagrid('destroyRow')">删除</a>
</c:if>
<a class="easyui-linkbutton"
data-options="iconCls:'icon-redo',plain:true"
href="${ctx}/fileupload/download_file_url?fileDir=download&fileName=${fileAll}">下载</a>//这里取不到值
<br />
<div id="search_gid"
style="margin-bottom: 4px; margin-top: 6px; font-size: 14px; padding-left: 5px; position: relative;">
<input name="filter_LIKES_name" class="easyui-textbox"
id="filter_LIKES_name"> <a class="easyui-linkbutton"
style="width: 80px;"
data-options="iconCls:'icon-search',plain:false"
onclick="javascript:ajax_search()">查询</a>
</div>
</div>
<div id="dlg" class="easyui-dialog"
style="width: 650px; height: 450px; padding: 10px 20px; top: 20px; background: #f5f5f5"
closed="true" buttons="#dlg-buttons_form">
<form id="fm" class="fm" method="post" novalidate
enctype="multipart/form-data">
<!-- <input name="id" type="hidden">-->
<div class="fitem">
<label> 插件排序: </label> <input name="sort" class="easyui-numberbox"
style="height: 30px;" data-options="required:true" />
</div>
<div class="fitem">
<label> 插件名称: </label> <input name="name" class="easyui-textbox"
id="filter_LIKES_name" style="height: 30px;" />
<!-- data-options="required:true,validType:['checkPname','checkPname']" -->
</div>
<div class="fitem">
<label> 插件上传: </label> <input type="file" id="upload" name="file"
style="height: 30px;" data-options="required:true" /> <img
id="test" style="display:none"
src="download" name="10202021" />
</form>
<div id="dlg-buttons_form">
<a id="savebutton" class="easyui-linkbutton" iconCls="icon-save"
onclick="javascript:$('#dg').edatagrid('save');save()"
style="width: 90px;">保存</a> <a class="easyui-linkbutton"
iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')"
style="width: 90px">取消</a>
</div>
<script type="text/javascript">
function save() {
var url = document.getElementById("test").src;
var name = document.getElementById("test").name;
alert(url);
alert(name);
}
//function save(){
//var file = document.getElementById("upload");
//alert(file);
//$.ajax({
// url : '${ctx}/6c/system/download/upload',
//type : 'post',
//data : {
//file : file
//},
//dataType : "json",
//success : function(result) {
//},
//error : function() {
//}
//});
//}
</script>
前端页面代码