gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:07:44 24 4
gpt4 key购买 nike

我想创建一个允许将文件上传到远程服务器的 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();
}
}

最佳答案

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

关于java - 使用 Java 类将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7893474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com