-

TLv8 文件处理

com.tlv8.core.utils.FileAndString

读取文件内容

java.io.File

String text = FileAndString.FileToString(file);

java.lang.StringBuffer

StringBuffer strbuf = FileAndString.FileToStringBuffer(file);

java.lang.String

String text = FileAndString.FileToString(filepath);

filepath文件路径


文本文件转换为指定编码的字符串

java.io.File

String text = FileAndString.file2String(file,encoding);

将字符串写入指定文件

String text = FileAndString.string2File(res,filePath);

res 原字符串

filePath 文件路径

String text = FileAndString.string2File(res,file);

res 原字符串

file 文件 java.io.File


将文件保存到文档服务

例子

Map m = new HashMap();
			m.put("sDocPath", DocPath);
			m.put("sDocName", DocName);
			Docinfo di = new Docinfo(m);
			AbstractDoc doca = new AbstractDoc(di);
			doca.upload(false, file);
			DocUtils.saveDocFlag(DocPath, doca.getsKind(), doca.getScacheName(), doca.getScacheName(), false);
			Map rem = new HashMap();
		rem.put("Kind", doca.getsKind());
		rem.put("cacheName", doca.getScacheName());
		rem.put("Size", doca.getsSize());
		rem.put("sDocName", DocName);

DocPath:文件保存位置(文档中心的路径并非物理路径,如:/root/合同附件)

DocName:原始文件名称,如:测试文件.doc

file:文件对象(java.io.File)


前端上传

@ResponseBody
	@RequestMapping(value = "/docUploadAction", method = RequestMethod.POST)
	public Object execute(@RequestParam("Filedata") MultipartFile file) throws Exception {
		try {
			String fileName = file.getOriginalFilename();
			Map<String, String> m = upLoadFiletoDaisy("/", fileName, file.getInputStream());
			Map<String, String> rm = new HashMap<>();
			rm.put("docName", fileName);
			rm.put("kind", file.getContentType());
			rm.put("size", String.valueOf(m.get("Size")));
			rm.put("cacheName", m.get("cacheName"));
			return rm;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "{}";
	}
	
	private Map<String, String> upLoadFiletoDaisy(String DocPath, String DocName, InputStream file) throws Exception {
		Map m = new HashMap();
		m.put("sDocPath", DocPath);
		m.put("sDocName", DocName);
		Docinfo di = new Docinfo(m);
		AbstractDoc doca = new AbstractDoc(di);
		doca.upload(false, file);
		DocUtils.saveDocFlag(DocPath, doca.getsKind(), doca.getScacheName(), doca.getScacheName(), false);
		Map rem = new HashMap();
		rem.put("Kind", doca.getsKind());
		rem.put("cacheName", doca.getScacheName());
		rem.put("Size", doca.getsSize());
		rem.put("sDocName", DocName);

		setKind(doca.getsKind());
		setFileID(doca.getScacheName());
		setSize(doca.getsSize());
		return rem;
	}

@RequestParam("Filedata") MultipartFile file Filedata:前端上传的字段名,可以根据自己的情况定义。


Powered By layui