gpt4 book ai didi

java - 如何从服务器端 Controller 返回或调用签名的小程序的方法?

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:04 24 4
gpt4 key购买 nike

我的下载场景是:当我单击 jsp 上的下载链接时,它会使用文件的 id 调用签名的 applet 方法,从 applet 中,我通过传递该 id 来调用服务器端方法。我可以在服务器端获取该文件,但我想将该文件返回/传递回我的小程序函数。我的问题是如何返回或将下载的文件传递到我的小程序?或者如何在服务器端将文件设置为响应对象,以便在小程序中有用?

我签名的小程序:

private static void downloadEncryptedFile(String uuid) throws HttpException, IOException {
String uri = "http://localhost:8080/encryptFileDownload.works?uuid="+uuid;
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
client.executeMethod(postMethod);
postMethod.releaseConnection();
}

我的服务器端功能:

@RequestMapping(value = "/encryptFileDownload/{uuid}.works", method = RequestMethod.POST)
public String downloadEncryptFile(@PathVariable("uuid") String uuid, HttpSession session, HttpServletResponse response) {
try {
if (StringUtils.isNotEmpty(uuid)) {
LOG.info("-----UUID----");
Node node = contentRetrieveService.getByNodeId(uuid);
Node resource = node.getNode("jcr:content");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + node.getName() + "\"");
InputStream in = resource.getProperty("jcr:data").getBinary().getStream();
ServletOutputStream outs = response.getOutputStream();
int i = 0;
while ((i = in.read()) != -1) {
outs.write(i);
}
outs.flush();
outs.close();
in.close();
LOG.info("File Downloaded");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

最佳答案

我找到了解决方案;我只是想传递一个文件 ID 下载它并将该文件返回到我的小程序,因此我对代码进行了更改:

我的小程序:

try {       
URL urlServlet = new URL("uri for your servlet");
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");

// send data to the servlet
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(uuid);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String name = con.getHeaderField("filename");
File fi = new File(name);
int i = 0;
while ((i = inputFromServlet.read()) != -1) {
System.out.println(inputFromServlet.readLine());
}
inputFromServlet.close();
instr.close();

} catch (Exception ex) {
ex.printStackTrace();
}

服务器端功能只需替换为:

OutputStream outs = response.getOutputStream();
outputToApplet = new ObjectOutputStream(outs);
int i = 0;
while ((i = in.read()) != -1) {
outputToApplet.write(i);
}

关于java - 如何从服务器端 Controller 返回或调用签名的小程序的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11899892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com