I'm a Java beginner, trying to get this to work in Eclipse. However, the readLine is struck through and a notice says it is deprecated. The code works, albeit not the while ((var2 = var5.readLine()) != null) { bit ... So I'm wondering how to fix it.

final class ScreenShotHelper$1 implements Runnable
{
    public void run()
    {
        try
        {
            String var1 = ScreenShotHelper.access$000().getAbsolutePath();
            String var2 = "";
            HttpURLConnection var3 = null;
            DataOutputStream var4 = null;
            DataInputStream var5 = null;
            String var6 = "\r\n";
            String var7 = "--";
            String var8 = "*****";
            String var9 = "";
            int var10 = 1048576;
            String var11 = "";
            var9 = Minecraft.getMinecraft().thePlayer.username;
            String var12 = "http://localhost/screenupload/index.php?playername=" + var9;
            try
            {
                FileInputStream var13 = new FileInputStream(new File(var1));
                URL var14 = new URL(var12);
                var3 = (HttpURLConnection)var14.openConnection();
                var3.setDoInput(true);
                var3.setDoOutput(true);
                var3.setUseCaches(false);
                var3.setRequestMethod("POST");
                var3.setRequestProperty("Connection", "Keep-Alive");
                var3.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + var8);
                var4 = new DataOutputStream(var3.getOutputStream());
                var4.writeBytes(var7 + var8 + var6);
                var4.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + var1 + "\"" + var6);
                var4.writeBytes(var6);
                int var15 = var13.available();
                int var16 = Math.min(var15, var10);
                byte[] var17 = new byte[var16];

                for (int var18 = var13.read(var17, 0, var16); var18 > 0; var18 = var13.read(var17, 0, var16))
                {
                    var4.write(var17, 0, var16);
                    var15 = var13.available();
                    var16 = Math.min(var15, var10);
                }
                var4.writeBytes(var6);
                var4.writeBytes(var7 + var8 + var7 + var6);
                System.out.println("File is written");
                var13.close();
                var4.flush();
                var4.close();
            }
            catch (MalformedURLException var20)
            {
                System.out.println("error1: " + var20.getMessage());
            }
            catch (IOException var21)
            {
                System.out.println("error2: " + var21.getMessage());
            }

            try
            {
                var5 = new DataInputStream(var3.getInputStream());

                while ((var2 = var5.readLine()) != null) {
                    System.out.println("Server Response " + var2);
                    ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7aSuccessfully uploaded screenshot!  Direct link:");
                    ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7a" + var2);
                }
                var5.close();
            }
            catch (IOException var19)
            {
                System.out.println("error3: " + var19.getMessage());
            }
        }
        catch (Exception var22)
        {
            var22.printStackTrace();
            ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a74failed to save");
        }
    }
}
有帮助吗?

解决方案

  • Give your variables meaningful names.
  • Declare your variables in the smallest possible scope.
  • Don't assign dummy values (like null or "") to variables.
  • Avoid side-effects in tests.
  • Use automatic resource management to cleanly ensure that streams are closed.
  • Determine and use the correct character encoding when converting bytes to characters.

Here is an example of your code that applies these points:

try (InputStream is = connection.getInputStream()) {
  BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  while (true) {
    String line = lines.readLine();
    if (line == null)
      break;
    System.out.println("Server Response " + line);
    ...
  }
}

其他提示

Use this code instead:

BufferedReader var5 = null;
try
{
    var5 = new BufferedReader(new InputStreamReader(var3.getInputStream()));
    // or perhaps
    // new BufferedReader(new InputStreamReader(var3.getInputStream(), "UTF-8"));
    while ((var2 = var5.readLine()) != null) {
        System.out.println("Server Response " + var2);
        ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7aSuccessfully uploaded screenshot!  Direct link:");
        ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7a" + var2);
    }

} finally {
    if (var5 != null) {
        try { var5.close(); }
        catch (IOException ignored) {}
    }
}

The reason for this (and the suggestion) can be found in the docs for DataInputStream.readLine():

This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method.

(If you're using Java 7, you can dispense with the finally clause by using a try-with-resources construct.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top