I tried changing @BalusC's great tutorial here to send gziped compressed files. This is the java working class:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPOutputStream;
public final class NetworkService {
private static final String FILENAME = "C:/Dropbox/TMP.txt";
private static final String URL =
"http://192.168.1.64:8080/DataCollectionServlet/";
private static final CharSequence CRLF = "\r\n";
private static boolean isServerGzip = true;
private static String charsetForMultipartHeaders = "UTF-8";
public static void main(String[] args) {
HttpURLConnection connection = null;
OutputStream serverOutputStream = null;
try {
File file = new File(FILENAME);
final String boundary = Long
.toHexString(System.currentTimeMillis());
connection = connection(true, boundary);
serverOutputStream = connection.getOutputStream();
try {
flushMultiPartData(file, serverOutputStream, boundary);
} catch (IOException e) {}
System.out.println(connection.getResponseCode());
} catch (IOException e) {
} finally {
if (connection != null) connection.disconnect();
}
}
private static HttpURLConnection connection(boolean isMultiPart,
String boundary) throws MalformedURLException, IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(URL)
.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) "
+ "Gecko/20100401");
connection.setChunkedStreamingMode(1024);
if (isMultiPart) {
if (boundary == null || "".equals(boundary.trim()))
throw new IllegalArgumentException("Boundary can't be "
+ ((boundary == null) ? "null" : "empty"));
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
}
return connection;
}
private static void flushMultiPartData(File file,
OutputStream serverOutputStream, String boundary)
throws IOException {
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(serverOutputStream,
charsetForMultipartHeaders), true);
appendBinary(file, boundary, writer, serverOutputStream);
writer.append("--" + boundary + "--").append(CRLF);
} finally {
if (writer != null) writer.close();
}
}
private static void appendBinary(File file, String boundary,
PrintWriter writer, OutputStream output)
throws FileNotFoundException, IOException {
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"binaryFile\"; filename=\""
+ file.getName() + "\"").append(CRLF);
writer.append(
"Content-Type: "
+ ((isServerGzip) ? "application/gzip" : URLConnection
.guessContentTypeFromName(file.getName())))
.append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
InputStream input = null;
OutputStream output2 = output;
if (isServerGzip) {
output2 = new GZIPOutputStream(output);
}
try {
input = new FileInputStream(file);
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output2.write(buffer, 0, length);
}
output2.flush();
} finally {
if (input != null) try {
input.close();
} catch (IOException logOrIgnore) {}
}
writer.append(CRLF).flush();
}
}
You need to edit the fields FILENAMEand URLconfigure the servlet in the URL - its method doPost():
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Collection<Part> parts = req.getParts();
for (Part part : parts) {
File save = new File(uploadsDirName, getFilename(part) + "_"
+ System.currentTimeMillis() + ".zip");
final String absolutePath = save.getAbsolutePath();
log.debug(absolutePath);
part.write(absolutePath);
sc.getRequestDispatcher(DATA_COLLECTION_JSP).forward(req, resp);
}
}
, isServerGzip true, FILENAME , , ( 7z , gzip , gzip, , , () ). - - , - , , - 0. , // ***. , gzipping ...?
, , .. .