1

我的下载场景是:当我单击 jsp 上的下载链接时,它调用带有文件 ID 的签名小程序方法,从小程序我通过传递该 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;
}
4

1 回答 1

1

我得到了解决方案;我只是想传递一个文件 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);
                }
于 2012-08-14T06:25:38.713 回答