MarkLogic:没有要写的流
MarkLogic: No stream to write
我遇到了一个问题,我有一个方法可以从 AngularJs 前端获取参数,用它们创建一个对象,将对象作为 XML 文件写入文件夹中,然后应该将该 XML 文件写入 MarkLogic 数据库。
但是,它应该写入数据库的部分看起来好像文件不存在,即使它确实存在:
代码如下:
@RequestMapping(value = "/add/korisnik", method = RequestMethod.POST)
public String addKorisnik(@RequestParam String ime, @RequestParam String prezime, @RequestParam String username, @RequestParam String password, @RequestParam String orcid, @RequestParam String role) throws JAXBException, FileNotFoundException{
Korisnik.Roles roles = new Korisnik.Roles();
roles.setRole(role);
Korisnik k = new Korisnik();
k.setIme(ime);
k.setPrezime(prezime);
k.setUsername(username);
k.setPassword(password);
k.setOrcid(orcid);
k.setRoles(roles);
System.out.println(k.toString());
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Korisnik.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
m.marshal(k, sw);
// Write to File
File f = new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml");
if (f.exists()) {
return "Username already taken.";
}
else {
m.marshal(k, new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml"));
}
// acquire the content
InputStream docStream = ObjavaNaucnihRadovaApplication.class.getClassLoader().getResourceAsStream(
"data/korisnici/" + k.getUsername() + ".xml");
// create the client
DatabaseClient client = DatabaseClientFactory.newClient(MarkLogicConfig.host,
MarkLogicConfig.port, MarkLogicConfig.admin,
MarkLogicConfig.password, MarkLogicConfig.authType);
// create a manager for XML documents
XMLDocumentManager docMgr = client.newXMLDocumentManager();
// create a handle on the content
InputStreamHandle handle = new InputStreamHandle(docStream);
// write the document content
docMgr.write("http://localhost:8011/korisnici/" + k.getUsername()+".xml", handle);
//release the client
client.release();
return "OK";
}
几个问题。
首先,您写入的文件与您读取的文件不同。您正在写入相对于 JVM(应用程序服务器)当前目录的 "src/main/resources/data/korisnici/.."。您正在从类路径资源目录中读取——不太可能相同。您可以简单地重复使用相同的文件对象,然后它们将是相同的。
其次,您不需要将这么小的对象写入磁盘,只需将其写入内存流(如 ByteArrayStream()
)。
类加载器似乎没有找到您的文件。
如果您已经有了刚刚编写的 File
对象,为什么不从中构造一个 InputStream
,而不是尝试从不同构造的相对路径中找到它?
InputStream docStream = new FileInputStream(f);
我遇到了一个问题,我有一个方法可以从 AngularJs 前端获取参数,用它们创建一个对象,将对象作为 XML 文件写入文件夹中,然后应该将该 XML 文件写入 MarkLogic 数据库。
但是,它应该写入数据库的部分看起来好像文件不存在,即使它确实存在:
代码如下:
@RequestMapping(value = "/add/korisnik", method = RequestMethod.POST)
public String addKorisnik(@RequestParam String ime, @RequestParam String prezime, @RequestParam String username, @RequestParam String password, @RequestParam String orcid, @RequestParam String role) throws JAXBException, FileNotFoundException{
Korisnik.Roles roles = new Korisnik.Roles();
roles.setRole(role);
Korisnik k = new Korisnik();
k.setIme(ime);
k.setPrezime(prezime);
k.setUsername(username);
k.setPassword(password);
k.setOrcid(orcid);
k.setRoles(roles);
System.out.println(k.toString());
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Korisnik.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
m.marshal(k, sw);
// Write to File
File f = new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml");
if (f.exists()) {
return "Username already taken.";
}
else {
m.marshal(k, new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml"));
}
// acquire the content
InputStream docStream = ObjavaNaucnihRadovaApplication.class.getClassLoader().getResourceAsStream(
"data/korisnici/" + k.getUsername() + ".xml");
// create the client
DatabaseClient client = DatabaseClientFactory.newClient(MarkLogicConfig.host,
MarkLogicConfig.port, MarkLogicConfig.admin,
MarkLogicConfig.password, MarkLogicConfig.authType);
// create a manager for XML documents
XMLDocumentManager docMgr = client.newXMLDocumentManager();
// create a handle on the content
InputStreamHandle handle = new InputStreamHandle(docStream);
// write the document content
docMgr.write("http://localhost:8011/korisnici/" + k.getUsername()+".xml", handle);
//release the client
client.release();
return "OK";
}
几个问题。
首先,您写入的文件与您读取的文件不同。您正在写入相对于 JVM(应用程序服务器)当前目录的 "src/main/resources/data/korisnici/.."。您正在从类路径资源目录中读取——不太可能相同。您可以简单地重复使用相同的文件对象,然后它们将是相同的。
其次,您不需要将这么小的对象写入磁盘,只需将其写入内存流(如
ByteArrayStream()
)。
类加载器似乎没有找到您的文件。
如果您已经有了刚刚编写的 File
对象,为什么不从中构造一个 InputStream
,而不是尝试从不同构造的相对路径中找到它?
InputStream docStream = new FileInputStream(f);