使用 Java 类将文件上传到服务器

发布于 2024-12-11 17:47:17 字数 8699 浏览 0 评论 0原文

我想创建一个允许将文件上传到远程服务器的 Java 应用程序(然后使其和小程序在网络上工作)。该文件之前已从服务器下载。我已经有了下载部分的代码,但对于上传部分,我有如下所示的代码。

导入库很好,但问题出在 Uploader 类上,它确实引发了错误.. 我认为我的 BufferReader 类缺少某些内容,但我需要您的帮助来调试它...

UploadApplet.java

import com.leo.upload.BufferReader;
import com.leo.upload.Uploader;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.LoggingPermission;
import java.util.logging.SimpleFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JApplet;
import javax.swing.JPanel;
import netscape.javascript.JSObject;

public class UploadApplet extends JApplet {


      public void start()
      {
        super.start();
        uploadFile("/Users/XXXXX/Desktop/Tesing123.indd","http://xx.xx.xxx.xxx/");
      }


     public String uploadFile(String paramString1, String paramString2)
      {
        String str = "0";
        try {
          // I call the Uploader class to make the upload to the server
              str = (String)AccessController.doPrivileged(new Uploader(paramString1, paramString2));
          System.out.println(str);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return str;
      }

Uploader.java

import com.leo.upload.BufferReader;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.PrivilegedAction;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Uploader
  implements PrivilegedAction<String>
{
  private static final String a = Uploader.class.getName();

  private Logger b = Logger.getLogger(a);
  private String c;
  private String d;

  public Uploader(String paramString1, String paramString2)
  {
    this.c = paramString1;
    this.d = paramString2;
  }

  public String run()
  {
    FileInputStream localFileInputStream = null;
    BufferedInputStream localBufferedInputStream = null;
    Writer localWriter = null;
    InputStream localInputStream = null;
    LineNumberReader localLineNumberReader = null;

    Object localObject1 = "0";
    try
    {
      File localFile = new File(this.c);
      localFileInputStream = new FileInputStream(localFile);
      localBufferedInputStream = new BufferedInputStream(localFileInputStream);

      Object localObject2 = new URL(this.d);
      URLConnection localURLConnection = ((URL)localObject2).openConnection();

      localURLConnection.addRequestProperty("Filename", localFile.getName());
      localURLConnection.setRequestProperty("Content-type", "application/binary");

      long l = localFile.length();
      Object localObject3;
      if (l <= 2147483647L) {
        if ((localURLConnection instanceof HttpURLConnection)) {
          localObject3 = (HttpURLConnection)localURLConnection;
          ((HttpURLConnection)localObject3).setFixedLengthStreamingMode((int)localFile.length());
        }

        localURLConnection.setDoOutput(true);

        **// I call the BufferReader class, and the a method to perform the writing**
        BufferReader.a(localBufferedInputStream, localURLConnection.getOutputStream());

        localInputStream = localURLConnection.getInputStream();
        localLineNumberReader = new LineNumberReader(new InputStreamReader(localInputStream, "UTF8"));
        localObject1 = localLineNumberReader.readLine();
      }
      else {
        localObject3 = "An error occurred during file upload: Cannot upload a file larger than 2GB.";
        JOptionPane.showMessageDialog(null, localObject3, "Error during file upload", 0);
        this.b.severe((String)localObject3);
        localObject1 = localObject3;
      }
    }
    catch (Throwable localIOException4) {
        localIOException4.printStackTrace();

      Object localObject2 = "An error occurred during file upload: " + localIOException4.toString();
      localObject1 = localObject2;
    } finally {
      BufferReader.a(localFileInputStream, localBufferedInputStream, localWriter);
      if (localLineNumberReader != null) {
        try {
          localLineNumberReader.close();
        } catch (IOException localIOException5) {
          localIOException5.printStackTrace();
        }
      }
      if (localInputStream != null) {
        try {
          localInputStream.close();
        } catch (IOException localIOException6) {
          localIOException6.printStackTrace();
        }
      }
    }
    return (String)(String)(String)localObject1;
  }


}

BufferReader.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
import java.util.logging.Logger;

public final class BufferReader
{
  private static Logger logthis = Logger.getLogger(BufferReader.class.getName());

  public static void a(InputStream paramInputStream, OutputStream paramOutputStream)
    throws IOException
  {
    byte[] arrayOfByte = new byte[4096];
    int i = 0;
    int j;
    while ((j = paramInputStream.read(arrayOfByte)) != -1) {
      paramOutputStream.write(arrayOfByte, 0, j);
      i += j;
    }
    logthis.fine("CopyBytes : " + i);
  }

  public static void a(InputStream paramInputStream, FileOutputStream paramFileOutputStream, BufferedOutputStream paramBufferedOutputStream)
  {
    if (paramInputStream != null) {
      try {
        paramInputStream.close();
      } catch (IOException localIOException1) {
        localIOException1.printStackTrace();
      }
    }

    if (paramFileOutputStream != null) {
      try {
        paramFileOutputStream.flush();
      } catch (IOException localIOException2) {
        localIOException2.printStackTrace();
      }
    }

    if (paramBufferedOutputStream != null) {
      try {
        paramBufferedOutputStream.flush();
      } catch (IOException localIOException3) {
        localIOException3.printStackTrace();
      }
    }
    if (paramFileOutputStream != null) {
      try {
        paramFileOutputStream.close();
      } catch (IOException localIOException4) {
        localIOException4.printStackTrace();
      }
    }
    if (paramBufferedOutputStream != null)
      try {
        paramBufferedOutputStream.close();
      } catch (IOException localIOException5) {
        localIOException5.printStackTrace();
      }
  }

  public static void a(InputStream paramInputStream, BufferedInputStream paramBufferedInputStream, Writer paramWriter)
  {
    if (paramBufferedInputStream != null)
      try {
        paramBufferedInputStream.close();
      }
      catch (IOException localIOException1)
      {
      }
    if (paramInputStream != null)
      try {
        paramInputStream.close();
      }
      catch (IOException localIOException2)
      {
      }
    if (paramWriter != null)
      try {
        paramWriter.close();
      }
      catch (IOException localIOException3)
      {
      }
  }

  public static String a(InputStream paramInputStream)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream, "UTF-8"));
    StringBuilder localStringBuilder = new StringBuilder();
    String str = null;

    while ((str = localBufferedReader.readLine()) != null) {
      localStringBuilder.append(str);
    }

    localBufferedReader.close();
    return localStringBuilder.toString();
  }
}

I want to create a Java application ( and then make it and applet to work on the web) that allows to upload a file to a remote server. The file has already been downloaded previously from the server. I have already have the code for the download part, but for the uploading I have the following code shown below..

The import libraries are fine but the issue is on the Uploader class that does throw me an error.. I think my BufferReader class is missing something but I need your help to debugging it...

UploadApplet.java

import com.leo.upload.BufferReader;
import com.leo.upload.Uploader;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.LoggingPermission;
import java.util.logging.SimpleFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JApplet;
import javax.swing.JPanel;
import netscape.javascript.JSObject;

public class UploadApplet extends JApplet {


      public void start()
      {
        super.start();
        uploadFile("/Users/XXXXX/Desktop/Tesing123.indd","http://xx.xx.xxx.xxx/");
      }


     public String uploadFile(String paramString1, String paramString2)
      {
        String str = "0";
        try {
          // I call the Uploader class to make the upload to the server
              str = (String)AccessController.doPrivileged(new Uploader(paramString1, paramString2));
          System.out.println(str);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return str;
      }

Uploader.java

import com.leo.upload.BufferReader;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.PrivilegedAction;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Uploader
  implements PrivilegedAction<String>
{
  private static final String a = Uploader.class.getName();

  private Logger b = Logger.getLogger(a);
  private String c;
  private String d;

  public Uploader(String paramString1, String paramString2)
  {
    this.c = paramString1;
    this.d = paramString2;
  }

  public String run()
  {
    FileInputStream localFileInputStream = null;
    BufferedInputStream localBufferedInputStream = null;
    Writer localWriter = null;
    InputStream localInputStream = null;
    LineNumberReader localLineNumberReader = null;

    Object localObject1 = "0";
    try
    {
      File localFile = new File(this.c);
      localFileInputStream = new FileInputStream(localFile);
      localBufferedInputStream = new BufferedInputStream(localFileInputStream);

      Object localObject2 = new URL(this.d);
      URLConnection localURLConnection = ((URL)localObject2).openConnection();

      localURLConnection.addRequestProperty("Filename", localFile.getName());
      localURLConnection.setRequestProperty("Content-type", "application/binary");

      long l = localFile.length();
      Object localObject3;
      if (l <= 2147483647L) {
        if ((localURLConnection instanceof HttpURLConnection)) {
          localObject3 = (HttpURLConnection)localURLConnection;
          ((HttpURLConnection)localObject3).setFixedLengthStreamingMode((int)localFile.length());
        }

        localURLConnection.setDoOutput(true);

        **// I call the BufferReader class, and the a method to perform the writing**
        BufferReader.a(localBufferedInputStream, localURLConnection.getOutputStream());

        localInputStream = localURLConnection.getInputStream();
        localLineNumberReader = new LineNumberReader(new InputStreamReader(localInputStream, "UTF8"));
        localObject1 = localLineNumberReader.readLine();
      }
      else {
        localObject3 = "An error occurred during file upload: Cannot upload a file larger than 2GB.";
        JOptionPane.showMessageDialog(null, localObject3, "Error during file upload", 0);
        this.b.severe((String)localObject3);
        localObject1 = localObject3;
      }
    }
    catch (Throwable localIOException4) {
        localIOException4.printStackTrace();

      Object localObject2 = "An error occurred during file upload: " + localIOException4.toString();
      localObject1 = localObject2;
    } finally {
      BufferReader.a(localFileInputStream, localBufferedInputStream, localWriter);
      if (localLineNumberReader != null) {
        try {
          localLineNumberReader.close();
        } catch (IOException localIOException5) {
          localIOException5.printStackTrace();
        }
      }
      if (localInputStream != null) {
        try {
          localInputStream.close();
        } catch (IOException localIOException6) {
          localIOException6.printStackTrace();
        }
      }
    }
    return (String)(String)(String)localObject1;
  }


}

BufferReader.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
import java.util.logging.Logger;

public final class BufferReader
{
  private static Logger logthis = Logger.getLogger(BufferReader.class.getName());

  public static void a(InputStream paramInputStream, OutputStream paramOutputStream)
    throws IOException
  {
    byte[] arrayOfByte = new byte[4096];
    int i = 0;
    int j;
    while ((j = paramInputStream.read(arrayOfByte)) != -1) {
      paramOutputStream.write(arrayOfByte, 0, j);
      i += j;
    }
    logthis.fine("CopyBytes : " + i);
  }

  public static void a(InputStream paramInputStream, FileOutputStream paramFileOutputStream, BufferedOutputStream paramBufferedOutputStream)
  {
    if (paramInputStream != null) {
      try {
        paramInputStream.close();
      } catch (IOException localIOException1) {
        localIOException1.printStackTrace();
      }
    }

    if (paramFileOutputStream != null) {
      try {
        paramFileOutputStream.flush();
      } catch (IOException localIOException2) {
        localIOException2.printStackTrace();
      }
    }

    if (paramBufferedOutputStream != null) {
      try {
        paramBufferedOutputStream.flush();
      } catch (IOException localIOException3) {
        localIOException3.printStackTrace();
      }
    }
    if (paramFileOutputStream != null) {
      try {
        paramFileOutputStream.close();
      } catch (IOException localIOException4) {
        localIOException4.printStackTrace();
      }
    }
    if (paramBufferedOutputStream != null)
      try {
        paramBufferedOutputStream.close();
      } catch (IOException localIOException5) {
        localIOException5.printStackTrace();
      }
  }

  public static void a(InputStream paramInputStream, BufferedInputStream paramBufferedInputStream, Writer paramWriter)
  {
    if (paramBufferedInputStream != null)
      try {
        paramBufferedInputStream.close();
      }
      catch (IOException localIOException1)
      {
      }
    if (paramInputStream != null)
      try {
        paramInputStream.close();
      }
      catch (IOException localIOException2)
      {
      }
    if (paramWriter != null)
      try {
        paramWriter.close();
      }
      catch (IOException localIOException3)
      {
      }
  }

  public static String a(InputStream paramInputStream)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream, "UTF-8"));
    StringBuilder localStringBuilder = new StringBuilder();
    String str = null;

    while ((str = localBufferedReader.readLine()) != null) {
      localStringBuilder.append(str);
    }

    localBufferedReader.close();
    return localStringBuilder.toString();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-12-18 17:47:17

请记住,未签名的小程序通常不允许打开与不是其原始 Web 服务器的地址的连接...除非您指定小程序可以在客户端的 java 控制面板中执行它们想要的任何操作

Bear in mind that unsigned applets aren't usually allowed to open connections to addresses that aren't their originating web server...unless you specify that applets can do whatever they want in the client's java control panel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文