欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  新闻

java导出word文档乱码(导出word文档案例)

程序员文章站 2022-10-26 11:50:47
公司数据迁移,一些附件无法直接导入到新系统,然后需要将附件从原系统下载到本地再上传到新系统,系统平台为 jira ,这里写了一个批量上传的代码,采用的是 curl 的方式,其中需要获取本地文件路径:p...

公司数据迁移,一些附件无法直接导入到新系统,然后需要将附件从原系统下载到本地再上传到新系统,系统平台为 jira ,这里写了一个批量上传的代码,采用的是 curl 的方式,其中需要获取本地文件路径:

public static arraylist<string> getfiles(string path) {
		arraylist<string> files = new arraylist<>();
		file file = new file(path);
		if (file.exists() && file.isdirectory() && file.canread()) {
			file[] templist = file.listfiles();
			for (int i = 0; i < templist.length; i++) {
				if (templist[i].isfile()) {
					files.add(templist[i].tostring());
				}
			}
			return files;
		}
		return null;
	}

这时候出现了一个问题,文件名包含中文,debug 的时候发现获取到的文件名出现了乱码

java导出word文档乱码(导出word文档案例)
java导出word文档乱码(导出word文档案例)

看了很多文章说需要转码,一一尝试没什么用,这时候获取本地编码方式发现出现了一个很奇怪的编码:cp1252,最后终于找到问题所在,是eclipse 默认编码的问题(别问我为什么不用 idea,公司不给用啊!),解决方式如图:

window –> preference –>

java导出word文档乱码(导出word文档案例)

将编码格式修改为 utf-8,再次运行程序就没有文件名乱码的问题了。

批量上传:

public static string execcurl(string issuekey, string file) {
		hashmap<string, string> info = getpropertiesutil.getinfo();
		string user = info.get("user");
		string password = info.get("password");
		string domain = info.get("domain");
		
		string[] cmds = { "curl", "-d-", "-u", user + ":" + password, "-x", "post", "-h", "x-atlassian-token: no-check",
				"-f", "file=@" + file, domain + issuekey + "/attachments" };
		processbuilder process = new processbuilder(cmds);
		process p;
		try {
			p = process.start();
			bufferedreader reader = new bufferedreader(new inputstreamreader(p.getinputstream()));
			stringbuilder builder = new stringbuilder();
			string line = null;
			while ((line = reader.readline()) != null) {
				builder.append(line);
				builder.append(system.getproperty("line.separator"));
			}
			return builder.tostring();

		} catch (ioexception e) {
			system.out.print("error");
			e.printstacktrace();
		}
		return null;
	}

注意:curl 命令要存到一个字符串数组内。