package com.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
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.JSONArray;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.operate.service.FileService;
import com.ssh.beans.Fileinfo;
import com.ssh.util.ExtHelper;
import com.ssh.util.SingleReturn;
public class FileOperAction extends ActionSupport
{
private static final long serialVersionUID = -5542083825742632187L;
private File fileData;
private String fileDataFileName;
private FileService fileService;
// 素材编号
private String fileNum;
// 素材名称
private String fileName;
// 素材路径
private String filePath;
// 素材大小
private String fileSize;
// 素材组编号
private String fileGid;
// 审核
private String fileCheck;
// 上传者编号
private String uploaderId;
// 上传时间
private String uploadTime;
public String preUpdateFile()
{
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().setAttribute("fileGroupId", request.getParameter("gid"));
request.getSession().setAttribute("fileType", request.getParameter("type"));
return SUCCESS;
}
/**
* 分布查询,查询出当前页面的信息
*
* @author wujiantao
* @return
* @throws Exception String
*/
@SuppressWarnings("unchecked")
public String getFileList() throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
// 获取起点和每页的限制
int start = Integer.parseInt(request.getParameter("start"));
int limit = Integer.parseInt(request.getParameter("limit"));
// 查询条件
Map map = new HashMap();
Map countmap = new HashMap();
int select = -1;
String fileGroupId = (String) request.getSession().getAttribute("fileGroupId");
map.put("fileGid", fileGroupId);
countmap.put("fileGid", fileGroupId);
// 如果有条件的话且均不为空的时候
if (!"".equals(request.getParameter("select")) && request.getParameter("select") != null
&& !"".equals(request.getParameter("content").trim())
&& request.getParameter("content").trim() != null)
{
select = Integer.valueOf(request.getParameter("select"));
String content = request.getParameter("content").trim();
/**
* 1、编号
* 2、用户名
* 3、权限
*/
switch (select)
{
case 1:
map.put("fileNum", content);
countmap.put("fileNum", content);
break;
case 2:
map.put("fileName", content);
countmap.put("fileName", content);
break;
case 3:
map.put("fileSize", content);
countmap.put("fileSize", content);
break;
case 4:
if ("未审核".equals(content))
{
map.put("fileCheck", 0);
countmap.put("fileCheck", 0);
}
else if ("审核通过".equals(content))
{
map.put("fileCheck", 1);
countmap.put("fileCheck", 1);
}
else if ("审核不通过".equals(content))
{
map.put("fileCheck", 2);
countmap.put("fileCheck", 2);
}
else
{
map.put("fileCheck", content);
countmap.put("fileCheck", content);
}
break;
case 5:
map.put("uploaderId", content);
countmap.put("uploaderId", content);
break;
default:
break;
}
}
try
{
// 根据起点和限制查询信息
List bookList = this.fileService.getFileByPage(start, limit, map);
// 查询满足条件的信息个数
int totalCount = this.fileService.getFileTotalCount(countmap);
JSONArray jsonArray = JSONArray.fromObject(bookList);
// 返回信息
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
response.getWriter().write("{results:" + totalCount + ",List:" + jsonArray + "}");
}
catch (Exception e)
{
throw new Exception("查询文件过程产生异常,操作失败!");
}
return null;
}
@SuppressWarnings("unchecked")
public String getAllFileinfo() throws IOException
{
List result = this.fileService.getAllFileinfo();
HttpServletResponse response = ServletActionContext.getResponse();
String xml = ExtHelper.getXmlFromList(result);
response.setContentType("application/xml;charset=UTF-8");
response.getWriter().write(xml);
return null;
}
/**
* 上传文件
*
* @author wujiantao
* @return String
*/
public String uploadFiles()
{
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getSession().getServletContext().getRealPath("/") + "upload";
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try
{
inputStream = new FileInputStream(getFileData());
outputStream = new FileOutputStream(new File(path, fileDataFileName));
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
// 添加数据库信息
String fileGroupId = (String) request.getSession().getAttribute("fileGroupId");
Fileinfo fileinfo = new Fileinfo();
// 0、未审核;1、审核通过;2、审核不通过
fileinfo.setFileCheck("0");
// 素材分组
fileinfo.setFileGid(fileGroupId);
// 素材名称:文件名称
fileinfo.setFileName(fileDataFileName);
// 素材编号
fileNum = this.fileService.getAvailFileNum();
fileinfo.setFileNum(fileNum);
// 路径
InetAddress inet = InetAddress.getLocalHost();
String addr = "http://" + inet.getHostAddress() + ":"+request.getLocalPort()+"/ssh_demo/upload/" + this.fileDataFileName;
fileinfo.setFilePath(addr);
// 文件大小
fileinfo.setFileSize(getFileSize(fileData));
// 上传时间
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
fileinfo.setUploadTime(dateFormat.format(now));
// 上传人
fileinfo.setUploaderId(((Integer)( request.getSession().getAttribute("userid"))).toString());
SingleReturn singleReturn = this.fileService.addFileUpload(fileinfo);
if (singleReturn.isSuccessful())
{
// 返回信息
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
response.getWriter().write("{success:true}");
}
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* 获取文件的大小
*
* @author wujiantao
* @return String
*/
public String getFileSize(File file)
{
String size = "0B";
// 如果文件存在
if (file.exists())
{
size = String.valueOf(file.length()) + "B";
// 按Kb算
long kSize = file.length() / 1024;
if (kSize > 0)
{
size = String.valueOf(kSize) + "K";
}
// 按Mb算
long mSize = file.length() / 1048576;
if (mSize > 0)
{
size = String.valueOf(mSize) + "M";
}
}
return size;
}
public String deleteFiles()
{
HttpServletRequest request = ServletActionContext.getRequest();
String[] fileIds = request.getParameterValues("fileIds");
boolean isSuccess = false;
for (int i = 0; i < fileIds.length; i++)
{
String fileId = fileIds[i];
this.fileService.deleteFile(fileId);
}
isSuccess = true;
HttpServletResponse response = ServletActionContext.getResponse();
response.setConte