发布时间:2026-07-25阅读(1)
公司数据迁移,一些附件无法直接导入到新系统,然后需要将附件从原系统下载到本地再上传到新系统,系统平台为 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 的时候发现获取到的文件名出现了乱码


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

将编码格式修改为 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 命令要存到一个字符串数组内。
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图