NC65 portal 网报 附件管理 附件上传、删除类nc.web.sscct.apct.controller.UPloadController
位于模块sscct下的public端
package nc.web.sscct.apct.controller;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nc.vo.erm.ErmFpRelationVO;
import nc.vo.pub.BusinessException;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.exception.ComponentException;
import nc.bs.logging.Logger;
import nc.imag.itf.IImagUtil;
import nc.itf.bd.pub.IBDMetaDataIDConst;
import nc.itf.erm.fprelation.IErmFPRelation;
import nc.itf.pub.web.IFiwebFileManagerService;
import nc.itf.pub.web.IWebPubService;
import nc.pubitf.bd.accessor.GeneralAccessorFactory;
import nc.uap.lfw.file.vo.LfwFileVO;
import nc.vo.bd.accessor.IBDData;
import nc.web.arap.controller.ArapFileManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import uap.iweb.action.BaseController;
import uap.iweb.render.RenderEngine;
import freemarker.template.SimpleHash;
@Controller
@RequestMapping(value = "/upload_ctr")
public class UPloadController extends BaseController {
private static MultipartResolver multipartResolver = new CommonsMultipartResolver();;
/**
* 获得MultipartHttpServletRequest
*
* @return
* @throws MultipartException
*/
private static MultipartHttpServletRequest getMultipartResolver(HttpServletRequest request) throws MultipartException {
((CommonsMultipartResolver) multipartResolver).setDefaultEncoding("UTF-8");
return multipartResolver.resolveMultipart(request);
}
@Autowired
RenderEngine renderEng;
/**
* 查询当前环节是否处在影像环节上
*
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings("restriction")
@RequestMapping(value = "/isworkflowimage", method = RequestMethod.GET)
public void isWorkFlowImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONObject json = new JSONObject();
try {
String pk_bill = request.getParameter("pk_bill");
boolean isworkImage = NCLocator.getInstance().lookup(IImagUtil.class).isWFOnImageActivity(pk_bill);
if (isworkImage) {
json.put("success", "true");
} else {
json.put("success", "false");
}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
} catch (Exception e) {
json.put("message", e.getMessage());
json.put("success", "false");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
Logger.error(e.getMessage(), e);
}
}
/**
* 上传附件
* @param file
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings({
"restriction", "unchecked" })
@RequestMapping(value = "/saveaccessory", method = RequestMethod.POST)
public void saveAccessory(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
String pk_bill = request.getParameter("pk_bill");
LfwFileVO[] files = NCLocator.getInstance().lookup(IFiwebFileManagerService.class).getLfwFileVOsByPath(pk_bill);
try {
if (files != null && files.length != 0) {
for (LfwFileVO existsFile : files) {
if (existsFile.getFilename().equals(file.getOriginalFilename())) {
throw new BusinessException("存在同名文件,请先删除后再上传");
}
}
}
ArapFileManager manage = new ArapFileManager();
manage.doUpload(file, request, response);
billManage(request, response);
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("success", "false");
json.put("message", e.getMessage());
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
Logger.error(e.getMessage(), e);
}
}
/**
* 获取附件
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings("restriction")
@RequestMapping(value = "/getaccessory", method = RequestMethod.POST)
public void billManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
String pk_bill = request.getParameter("pk_bill");
// String state = req.getParameter("state");
SimpleHash root = new SimpleHash();
LfwFileVO[] files = NCLocator.getInstance().lookup(IFiwebFileManagerService.class).getLfwFileVOsByPath(pk_bill);
JSONObject json = new JSONObject();
JSONObject json2 = new JSONObject();
ArrayList<JSONObject> list = new ArrayList<JSONObject>();
// 获取电子发票附件
ErmFpRelationVO[] relationVOs = NCLocator.getInstance().lookup(IErmFPRelation.class).getRelations(pk_bill);
Map<String, ErmFpRelationVO> relationVOMap = new HashMap<String, ErmFpRelationVO>();
for (ErmFpRelationVO realiton : relationVOs) {
relationVOMap.put(realiton.getPk_bxd(), realiton);
}
for (LfwFileVO file : files) {
if (relationVOMap.get(file.getExt1()) != null) {
file.setExt2("isDZFP");
} else {
file.setExt2("notDZFP");
}
String ext1 = file.getExt1();
ext1 = ext1.substring(0, ext1.lastIndexOf("/")) + "|" + ext1.substring(ext1.lastIndexOf("/") + 1, ext1.length());
file.setExt1(ext1);
IBDData userData = GeneralAccessorFactory.getAccessor(IBDMetaDataIDConst.USER).getDocByPk(file.getCreator());
if (userData != null) {
file.setCreator(userData.getName().toString());
}
JSONObject jsonfile = new JSONObject();
jsonfile.put("creattime", file.getCreattime().toString());
jsonfile.put("filename", file.getFilename());
jsonfile.put("creator", file.getCreator());
jsonfile.put("ext1", file.getExt1());
jsonfile.put("ext2", file.getExt2());
jsonfile.put("pk_lfwfile", file.getPk_lfwfile());
list.add(jsonfile);
}
JSONArray jsonlist = JSONArray.fromObject(list);
json2.put("list", jsonlist);
// root.put("accessorys", files);
json2.put("pk_bill", pk_bill);
// json.put("state", state);
json.put("data", json2);
json.put("success", "true");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
// String html = renderEng.render("/billtemplate/structure/arapcard.html", root);
// print(response, html);
} catch (Exception e) {
SimpleHash root = new SimpleHash();
root.put("message", e.getMessage());
// String html = renderEng.render("/billtemplate/structure/arapcard.html", root);
// print(response, html);
Logger.error(e.getMessage(), e);
}
}
/**
* 删除附件
* @param request
* @param response
* @throws Exception
*/
@SuppressWarnings("restriction")
@RequestMapping(value = "/deletefile", method = RequestMethod.POST)
public void delAccessory(HttpServletRequest request, HttpServletResponse response) throws Exception {
// MultipartHttpServletRequest req = getMultipartResolver(request);
String fileNo = request.getParameter("filepk");
JSONObject json = new JSONObject();
try {
// if (null != fileNo && !fileNo.equals("")) {
// fileNo = URLDecoder.decode(fileNo, "utf-8");
// }
FiwebFileManager manage = new FiwebFileManager();
NCLocator.getInstance().lookup(IFiwebFileManagerService.class).doDelete(fileNo);
// manage.doDelete(request, response);
// this.getIWebPubService()
// .updateFPForDR(new String[] { fileNo }, "1");
json.put("success", "true");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
} catch (Exception e) {
json.put("message", e.getMessage());
json.put("success", "false");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
response.flushBuffer();
Logger.error(e.getMessage(), e);
}
// billManage(request, response);
}
protected IWebPubService getIWebPubService() throws ComponentException {
return NCLocator.getInstance().lookup(IWebPubService.class);
}
}
package nc.web.sscct.apct.controller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nc.bs.dao.BaseDAO;
import nc.bs.er.filemanager.adapter.NCFileManagerAdapter;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.filesystem.FileSystemUtil;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.itf.pub.web.IFiwebFileManagerService;
import nc.jdbc.framework.SQLParameter;
import nc.uap.lfw.core.exception.LfwRuntimeException;
import nc.uap.lfw.file.vo.LfwFileVO;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.pub.filesystem.NCFileVO;
import nc.web.sscct.utils.NCFileVOResultSetProcessor;
public class FiwebFileManager implements IFiwebFileManagerService {
@SuppressWarnings("restriction")
@Override
public void doUpload(byte[] paraInput, String fileName, long formDataLength, String billItem) {
// 定义上载文件的最大字节
int MAX_SIZE = 102400 * 102400;
// DataInputStream in = null;
// String fileName = f.getOriginalFilename();
try {
// if (!f.isEmpty()) {
// in = new DataInputStream(f.getInputStream());
// byte[] paraInput = IOUtils.toByteArray(in);
// // 读入上传的数据
// String billItem=request.getParameter("pk_bill");
// long formDataLength = f.getSize();
if (formDataLength > MAX_SIZE) {
throw new LfwRuntimeException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("sftemp", "UploadAction-0002", null, new String[] {
fileName })/* {0}大小超过限制不能上传! */);
}
InputStream input = new ByteArrayInputStream(paraInput);
NCLocator.getInstance().lookup(IFileSystemService.class).createNewFileNodeWithStream(billItem, fileName, InvocationInfoProxy.getInstance().getUserId(), input, formDataLength);
// } else {
// throw new BusinessRuntimeException("不支持的文件上传类型");
// }
} catch (Exception ex) {
throw new BusinessRuntimeException(ex.getMessage());
}
}
@SuppressWarnings("restriction")
@Override
public void doDelete(String fileNo) {
// 定义上载文件的最大字节
try {
// String fileNo=request.getParameter("filepk");
// fileNo = URLDecoder.decode(fileNo, "utf-8");
new NCFileManagerAdapter().delete(fileNo);
} catch (Exception ex) {
throw new BusinessRuntimeException(ex.getMessage());
}
}
@SuppressWarnings("restriction")
@Override
public LfwFileVO[] doQuery(HttpServletRequest request, HttpServletResponse response) {
// 定义上载文件的最大字节
try {
String billItem = request.getParameter("pk_bill");
return new NCFileManagerAdapter().getFileQryService().getFile(null, billItem);
} catch (Exception ex) {
throw new BusinessRuntimeException(ex.getMessage());
}
}
@Override
public LfwFileVO[] doQuery(String pk_bill) {
// 定义上载文件的最大字节
try {
return new NCFileManagerAdapter().getFileQryService().getFile(null, pk_bill);
} catch (Exception ex) {
throw new BusinessRuntimeException(ex.getMessage());
}
}
@Override
public LfwFileVO[] getLfwFileVOsByPath(String path) throws Exception {
LfwFileVO[] lfwFileVOs = new NCFileManagerAdapter().getFileQryService().getFile(null, path);
NCFileVO[] ncFileVOs = this.queryFileVOsByPath(path, false);
List<LfwFileVO> lfwFileVOList = new ArrayList<LfwFileVO>();
Set<String> ncFileVOSet = new HashSet<String>();
for (NCFileVO filevo : ncFileVOs) {
ncFileVOSet.add(filevo.getFullPath());
}
for (LfwFileVO lfwFileVO : lfwFileVOs) {
if (ncFileVOSet.contains(lfwFileVO.getExt1())) {
lfwFileVOList.add(lfwFileVO);
}
}
return lfwFileVOList.toArray(new LfwFileVO[0]);
}
public NCFileVO[] queryFileVOsByPath(String path, Boolean isAll) throws Exception {
path = FileSystemUtil.validatePathString(path);
// String sql =
// "select pk, filepath, creator,isfolder,lasttime,filelength from
// sm_pub_filesystem where filepath=? or filepath like ?";
// 添加isdoc字段,2011-9-19
// v63 添加4个字段(modifier,filedesc,scantimes,modifytime) 2012-10-23
// v63 又加入filetype 字段表示文件类型,区别url,image等类型 不同浏览
String sql = "select pk, filepath, creator,isfolder,lasttime,filelength,isdoc,pk_doc,modifier,filedesc,scantimes,modifytime,filetype from sm_pub_filesystem where filepath=? or filepath like ?";
if (!isAll) {
sql = sql + " and dr = 0";
}
SQLParameter param = new SQLParameter();
param.addParam(path);
// if (!path.endsWith("/"))
// path = path + "/";
param.addParam(path + "/%");
BaseDAO dao = new BaseDAO();
List<NCFileVO> list = (List<NCFileVO>) dao.executeQuery(sql, param, new NCFileVOResultSetProcessor());
return list.toArray(new NCFileVO[0]);
}
}
package nc.impl.pub.filesystem;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import uap.sec.esapi.UAPESAPI;
import javax.imageio.ImageIO;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.logging.Logger;
import nc.bs.ml.NCLangResOnserver;
import nc.bs.pub.filesystem.FileSystemUtil;
import nc.bs.pub.filesystem.IAttachManageConst;
import nc.itf.org.IOrgConstBasic;
import nc.jdbc.framework.JdbcSession;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.crossdb.CrossDBConnection;
import nc.jdbc.framework.crossdb.CrossDBPreparedStatement;
import nc.jdbc.framework.crossdb.nativejdbc.NativeJdbcExtractorFactory;
import nc.jdbc.framework.exception.DbException;
import nc.jdbc.framework.exception.