【发布时间】:2021-12-06 14:05:42
【问题描述】:
我有一个应用程序,它是服务器上的一个 API(比如 192.168.0.2),可以将文件(任何格式)上传到或从中下载。
如果网络上另一台机器(例如 192.196.0.3)上的应用程序想要将文件上传到 API,它会以 JSON 对象的形式传递信息,例如{ "FILE_LOCATION":"file:/192.168.0.3/c:/testDocs/testFile.docx" }
api中的代码大致如下:
private static void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String errorMessage = "";
try
{
String src = request.getParameter ("src");
Object obj = jsonParser.parse (src);
JSONObject jsonObj = (JSONObject) obj;
String fileLocation = (String) jsonObj.get ("FILE_LOCATION");
URI uri = new URI (fileLocation);
URL url = uri.toURL (); // get URL from your uri object
URLConnection urlConnection = url.openConnection ();
InputStream is = urlConnection.getInputStream ();
System.out.println ("InputStream = " + is);
if (is != null)
{
// create output file, output stream etc
}
}
catch (Exception e)
{
errorMessage = e.getMessage ();
System.out.println (e.getClass().getName () + " : " + errorMessage);
}
PrintWriter pw = response.getWriter ();
pw.append (errorMessage);
}
系统日志总是显示如下内容: "java.io.FileNotFoundException : 192.168.0.3/c:/testDocs/testFile.docx (没有这样的文件或目录)"
我做错了什么?我确信错误在于我构建将用于创建 URI 的字符串的方式。
【问题讨论】: