package com.yibei.controller.admin;
/** 文件上传controller
* @author 陈伟 E-mail: zj695418816@gmail.com
* @version 创建时间:2017年7月6日 下午2:28:08
* 类说明
*/
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.yibei.entity.App;
import com.yibei.entity.PageBean;
import com.yibei.service.AppService;
import com.yibei.service.TypeService;
import com.yibei.util.DateUtil;
import com.yibei.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@Controller
@RequestMapping("admin/apk")
public class AppAdminController {
@Resource
private AppService appService;
@Resource
private TypeService typeService;
/**
* 上传apk文件
* @param apkFile
* @param app
* @param request
*/
private void upApk(MultipartFile apkFile,App app,HttpServletRequest request){
if(apkFile.isEmpty()){
System.out.println("未选择上传的apk文件");
}else{
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/"+app.getType().getDir());
try {
FileUtils.copyInputStreamToFile(apkFile.getInputStream(), new File(realPath,apkFile.getOriginalFilename()));
app.setSize(apkFile.getSize());
app.setFullName(apkFile.getOriginalFilename());
System.out.println("apk文件上传成功 ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传apk出现异常");
}
}
}
/**
* 上传图标
* @param iconFile
* @param app
* @param request
* @throws Exception
*/
private void upIcon(MultipartFile iconFile,App app,HttpServletRequest request) throws Exception{
if(iconFile.isEmpty()){
System.out.println("未选择上传的apk图标");
}else{
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/icon");
try {
String suffix = iconFile.getOriginalFilename().substring(iconFile.getOriginalFilename().lastIndexOf(".")+1);
String fullName =DateUtil.getCurrentDateStr()+"."+suffix;
FileUtils.copyInputStreamToFile(iconFile.getInputStream(), new File(realPath,fullName));
app.setIcon(fullName);
System.out.println("图标上传成功 ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传图标出现异常");
}
}
}
/**
* 实现apk的下载
* @param id
* @param response
* @param request
*/
@RequestMapping("load")
public void download(@RequestParam Integer id,HttpServletResponse response,HttpServletRequest request) {
OutputStream os =null;
if(id!=null){
App app = appService.getById(id);
try {
os = response.getOutputStream();
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(app.getFullName(),"utf-8"));
os.write(FileUtils.readFileToByteArray(getDictionaryFile(request,app)));
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os !=null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(app==null) return ;
}
}
private File getDictionaryFile(HttpServletRequest request,App app){
File file = new File(request.getSession().getServletContext().getRealPath("/WEB-INF/"+app.getType().getDir()+"\\"+app.getFullName()));
System.out.println(request.getSession().getServletContext().getRealPath("/WEB-INF/"+app.getType().getDir()+"\\"+app.getFullName()));
return file;
}
/**
* 罗列服务端上传的apk记录
* @param page
* @param rows
* @param response
* @return
* @throws Exception
*/
@RequestMapping("list")
public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
Map<String,Object> map = new HashMap<String,Object>();
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
List<App> appList = appService.getAppList(map);
Long total = appService.getTotal();
JSONObject result = new JSONObject();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
JSONArray jsonArray = JSONArray.fromObject(appList, jsonConfig);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(response, result);
return null;
}
/**
* 更新或添加app记录
* @param app
* @param apkFile
* @param iconFile
* @param response
* @param request
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(App app,@RequestParam MultipartFile apkFile,@RequestParam MultipartFile iconFile,HttpServletResponse response,HttpServletRequest request)throws Exception{
int resultTotal=0;
JSONObject result=new JSONObject();
if(!iconFile.isEmpty()){
this.upIcon(iconFile, app, request);
}
if(app.getType().getId()==null) {
result.put("success", false);
ResponseUtil.write(response, result);
return null;
}
app.setType(typeService.getById(app.getType().getId()));
if(app.getId()==null){
this.upApk(apkFile, app, request);
resultTotal=appService.add(app);
}else{
this.upApk(apkFile, app, request);
resultTotal = appService.update(app);
}
if(resultTotal>0){
result.put("success", true);
}else{
result.put("success", false);
}
ResponseUtil.write(response, result);
return null;
}
/**
* 删除app记录
* @param ids
* @param response
* @param request
* @return
* @throws Exception
*/
@RequestMapping("/delete")
public String delete(@RequestParam(value="ids",required=false)String ids,HttpServletResponse response,HttpServletRequest request)throws Exception{
JSONObject result = new JSONObject();
String []idsStr = ids.split(",");
for(int i=0;i<idsStr.length;i++){
App app = appService.getById(Integer.parseInt(idsStr[i]));
this.deleteApk(app, request);
appService.delete(Integer.parseInt(idsStr[i]));
}
result.put("success", true);
ResponseUtil.write(response, result);
return null;
}
/**
* 使得前台接收的String类型的日期能够自动封装到实体bean中Date类型的属性字段中
* @param request
* @param binder
*/
@InitBinder
protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
/**
* 在删除app记录的同时将存储在服务端的apk删除
* @param app
* @param request
*/
private void deleteApk(App app ,HttpServletRequest request){
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/"+app.getType().getDir());
FileUtils.deleteQuietly(new File(realPath+"\\"+app.getFullName()));
System.out.println("文件已删除");
}
}
实体bean
package com.yibei.entity;
import java.util.Date;
/**
* app实体
* @author cielab
*
*/
public class App {
private Integer id;
private String name;
private String suffix;
private Type type;
private String version;
private String des;
private float size;
private String icon;
private String log;
private String info;
private Date release_date;
private String fullName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getLog() {
return log;
}
public void setLog(String log) {
this.log = log;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getRelease_date() {
return release_date;
}
public void setRelease_date(Date release_date) {
this.release_date = release_date;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
package com.yibei.entity;
/**
* 应用类别实体
* @author 陈伟 E-mail: zj695418816@gmail.com
* @version 创建时间:2017年7月7日 上午8:48:25
* 类说明
*/
public class Type {
private Integer id;
private String name;
private String dir;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
}
package com.yibei.entity;
/**
*
* @author
*
*/
public class PageBean {
private int page;
private int pageSize;
private int start;
public PageBean(int page, int pageSize) {
super();
this.page = page;
this.pageSize = pageSize;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStart() {
return (page-1)*pageSize;
}
}