2011-10-25 75 views
0

我想創建一個Java應用程序(然後使它和applet在Web上工作),它允許將文件上傳到遠程服務器。該文件以前已從服務器下載過。我已經有下載部分的代碼,但對於上傳我有以下代碼如下所示..使用Java類上傳文件到服務器

導入庫是好的,但問題是在上傳類,確實會給我一個錯誤。我想我的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; 
     } 

Uploade r.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(); 
    } 
} 
+3

什麼是錯誤?給我們堆棧跟蹤。 – AlexR

+1

方法和變量的單字母名稱是什麼?如果您想混淆二進制文件,請編寫普通代碼並在類文件上的混淆器上使用。 – jackrabbit

回答

2

記住,未簽名的Applet通常不會允許開到不屬於他們發起的網絡服務器地址的連接...除非您指定applet可以在客戶端的java控制面板中執行任何操作

+0

實際上,我希望此代碼正確行爲,然後將其轉換爲小程序。如果我從eclipse開啓它,它應該可以正常工作......關於在我的代碼被卡住的這些行上可能會發生什麼的任何想法? localInputStream = localURLConnection.getInputStream(); localLineNumberReader = new LineNumberReader(new InputStreamReader(localInputStream,「UTF8」)); localObject1 = localLineNumberReader.readLine(); – Leo

+0

@Leo我們不能幫助您,直到您向我們展示異常跟蹤是什麼!!! ...您是否推斷代碼在該行被阻止,或者您是否收到異常? –