如何在JSP(java)中实现PHP file_get_contents()函数?

如何在JSP(java)中实现PHP file_get_contents()函数?,java,php,jsp,Java,Php,Jsp,在PHP中,我们可以像这样使用file\u get\u contents(): <?php $data = file_get_contents('php://input'); echo file_put_contents("image.jpg", $data); ?> 如何在Java(JSP)中实现这一点?这是我不久前用Java创建的一个函数,它返回一个文件内容字符串。希望能有帮助 与\n和\r可能有一些问题,但至少应该让您开始 // Converts a file

在PHP中,我们可以像这样使用
file\u get\u contents()

<?php

  $data = file_get_contents('php://input');
  echo file_put_contents("image.jpg", $data);

?>


如何在Java(JSP)中实现这一点?

这是我不久前用Java创建的一个函数,它返回一个文件内容字符串。希望能有帮助

与\n和\r可能有一些问题,但至少应该让您开始

// Converts a file to a string
private String fileToString(String filename) throws IOException
{
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    StringBuilder builder = new StringBuilder();
    String line;    

    // For every line in the file, append it to the string builder
    while((line = reader.readLine()) != null)
    {
        builder.append(line);
    }

    reader.close();
    return builder.toString();
}

这将从URL读取文件并将其写入本地文件。只需根据需要添加try/catch和导入

   byte buf[] = new byte[4096];
   URL url = new URL("http://path.to.file");
   BufferedInputStream bis = new BufferedInputStream(url.openStream());
   FileOutputStream fos = new FileOutputStream(target_filename);

   int bytesRead = 0;

   while((bytesRead = bis.read(buf)) != -1) {
       fos.write(buf, 0, bytesRead);
   }

   fos.flush();
   fos.close();
   bis.close();

谢谢,但我能拿到
php://input
<代码>文件字符串(“php://input“”返回JSPIf中的java.io.FileNotFoundException如果需要原始输入流,则可以使用。request.getInputStream();我不能改变
php://input
,这是一个闪光灯,我怎样才能得到
php://input
在JSP中?