2、后台DS方法
//上传DS中的方法
public TmDocAccessory uploadAccessoryPublic(TmDocAccessory tmd,User user,FormFile fileList)
{
InputStream isr = null;
OutputStream os = null;
//SmbFileOutputStream fo=null;
DocumentBuilder builder;
String path = "";
TmDocAccessory tmda = new TmDocAccessory();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(this.getClass()
.getClassLoader().getResource("/LoadFilePath.xml")
.getPath()));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Header");
Element element = (Element) list.item(0);
path = element.getChildNodes().item(0).getNodeValue();
//用于上传附件到共享目录
//SmbFile uploadFile = new SmbFile("smb://administrator:server254639@192.168.0.254/upload_file/");
//SmbFile uploadFile = new SmbFile(path);
File uploadFile= new File(path);
// SmbFile uploadFile = new SmbFile(rmifile); //指定上传文件的位置
if (!uploadFile.exists() || uploadFile == null) { // 判断指定路径dir是否存在,不存在则创建路径
uploadFile.mkdirs();
}
FormFile tmp = fileList;
isr = null;
Long randomNum = new Random().nextLong();
String radomStr = randomNum.toString().substring(2, 5);
String accessoryAutoName = new Date().getTime() + radomStr;
byte[] cbuf = new byte[1024];
// tmp = (FormFile) iter.next();
String extendedName = this.splitString(tmp.getFileName(), ".");
String oldName = this.splitStringPath(tmp.getFileName());
// String oldName = this
// .splitStringPath(tmp.getName(),File.separator);
String fileName = accessoryAutoName;
if (extendedName != null) {
fileName = accessoryAutoName + "." + extendedName;
}
//设置关联id 也就是文档id
tmda.setTmDocTypeId(tmd.getTmDocTypeId());
tmda.setAccessoryFileName(fileName);
tmda.setAccessoryOldFileName(oldName);
int size = tmp.getFileSize();
tmda.setAccessorySize(new Integer(size));
tmda.setExtendedName(extendedName);
tmda.setMemo(tmd.getMemo());
tmda.setAccessoryTitle(tmd.getAccessoryTitle());
tmda.setCreatedByEmployee(user.getUserId());
tmda.setCreatedByDept(user.getDeptid());
Timestamp ts = new Timestamp(System.currentTimeMillis());
tmda.setCreationDate(ts);
tmda.setLastUpdatedByEmployee(user.getUserId());
tmda.setLastUpdatedByDept(user.getDeptid());
tmda.setLastUpdateDate(ts);
// 保存数据到附件表
this.insertTmDocAccessory(tmda);
isr = tmp.getInputStream();
System.out.println("path======"+uploadFile.getPath());
os = new FileOutputStream(uploadFile.getPath() + File.separator
+ fileName);
//fo= new SmbFileOutputStream(uploadFile.getPath() + File.separator+ fileName);
int i = 0;
while ((i = isr.read(cbuf, 0, 1024)) != -1) {
os.write(cbuf, 0, i);
//fo.write(cbuf, 0, i);
}
// }
} catch (Exception e)
{
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
isr = null;
} catch (Exception e) {
}
}
if (os != null) {
try {
os.close();
os = null;
} catch (Exception e) {
}
}
/* if (fo != null) {
try {
fo.close();
fo = null;
} catch (Exception e) {
}
}*/
}
return tmda;
}
//下载方法
public ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
TmFolderForm theForm = (TmFolderForm) form;
/*String accIdStr=request.getParameter("tmDocAccessoryId");
Integer accId=0;
if(accIdStr!=null)
{
accId=Integer.parseInt(accIdStr.trim().toString());
}*/
Integer accId = theForm.getTmDocAccessoryId();
TmDocAccessory accessoryV =tmDocAccessoryDS.getTmDocAccessory(accId);
System.out.println("accessoryV======"+accessoryV+"accID"+accId);
String fullName = accessoryV.getAccessoryOldFileName();
String fileName=accessoryV.getAccessoryFileName();
//String filePath=accessoryV.getAccessoryActualPath();
//SmbFileOutputStream fo=null;
DocumentBuilder builder;
String filePath="";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(this.getClass()
.getClassLoader().getResource("/LoadFilePath.xml")
.getPath()));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("Header");
Element element = (Element) list.item(0);
filePath = element.getChildNodes().item(0).getNodeValue();
} catch (RuntimeException e1) {
e1.printStackTrace();
}
File uploadFile = new File("D:/upload_file/"+fileName); //指定上传文件的位置
//SmbFile uploadFile=new SmbFile(filePath+fileName); //指定上传文件的位置
if (!uploadFile.exists() || uploadFile == null)
{
//PrintWriter out=response.getWriter();
//out.print("<script> kk();</script>");
//out.close();
return (mapping.findForward("uploadError"));
}
else
{
FileInputStream fis=null;
fis = new FileInputStream(filePath + fileName);
//SmbFileInputStream fis=null;
//fis=new SmbFileInputStream(filePath+fileName);
BufferedInputStream bis=new BufferedInputStream(fis);
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=/""+new String(fullName.getBytes("gb2312"),"ISO8859-1")+"/"");
OutputStream os=response.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(os);
byte[]cbuf=new byte[1024];
while(bis.read(cbuf)!=-1){
bos.write(cbuf);
}
bos.flush();
try{
bos.close();
} catch (IOException e){
e.printStackTrace();
}
try{
bis.close();
} catch (IOException e){
e.printStackTrace();
}
}
return null;
}
3、/LoadFilePath.xml
<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Header left="100" Top="30">D:/upload_file/</Header>
<ServiceType>2</ServiceType>
</Root>