使用HTTP从Java客户端上传Java文件
我的客户机代码是用java编写的。它根据请求将文件上传到不同的服务器。这些请求在印度到美国、美国到英国等国家之间传递。此外,有时文件大到2GB。我当前的代码是无效的,因为它上传文件的速度很慢,并且需要您提供1GB的堆空间,即使只上传60MB的文件使用HTTP从Java客户端上传Java文件,java,file-upload,Java,File Upload,我的客户机代码是用java编写的。它根据请求将文件上传到不同的服务器。这些请求在印度到美国、美国到英国等国家之间传递。此外,有时文件大到2GB。我当前的代码是无效的,因为它上传文件的速度很慢,并且需要您提供1GB的堆空间,即使只上传60MB的文件 String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, b
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
//System.out.println("Uploading the file");
String responseFromServer = "";
String urlString = "http://stcss.us.com:8888/codesign/UploadServlet";
try {
//------------------ CLIENT REQUEST
FileInputStream fileInputStream =
new FileInputStream(new File(exsistingFileName));
int file_size = fileInputStream.available();
if (file_size > 1000 * 1024 * 1024) {
System.out.println("File Size Error\n Max Size allowed is 500MB");
System.exit(1);
}
// open a URL connection to the Jsp
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection)url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" +
boundary);
//conn.setRequestProperty("Accept", "application/octet-stream");
conn.setRequestProperty("password", password);
conn.setRequestProperty("signingParameters", signingParameters);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" +
" filename=\"" + exsistingFileName + "\"" +
lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
// System.out.println ("file size is"+file_size+"avaiable is"+bytesAvailable);
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
System.out.println("From CLIENT CLIENT REQUEST Malformed:" + ex);
System.exit(0);
} catch (IOException ioe) {
System.out.println("From CLIENT CLIENT REQUEST:" + ioe);
System.exit(0);
}
我可以通过哪些方式更改此代码以加快上载速度,并为相当小的文件消除堆空间?服务器端是否需要更改某些内容?我在服务器端使用apache commons fileupload。为什么不使用Http组件之类的东西,这样就不必像其他人以前那样做了;我听说java.nio提供了更快的上传速度。这是真的吗?如何使用它?