I just want to send a text file and a JPEG file over the network. fortunately, i have access to both the server code and the client code. Here's my (google app engine) code.
private void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); GcsFilename filename = new GcsFilename("my-bucket", "my-file"); Builder fileOptionsBuilder = new GcsFileOptions.Builder(); fileOptionsBuilder.mimeType("text/html"); GcsFileOptions fileOptions = fileOptionsBuilder.build(); GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions); byte[] buffer = new byte[1024]; InputStream reader = req.getInputStream(); BufferedOutputStream outStream = new BufferedOutputStream(Channels.newOutputStream(outputChannel)); while(true) { int bytesRead = reader.read(buffer); if (bytesRead == -1) { break; // have a break up with the loop. } else if (bytesRead < 1024) { byte[] temp = Arrays.copyOf(buffer, bytesRead); outStream.write(temp); } else { outStream.write(buffer); } } outStream.close(); outputChannel.close(); } As you can see, i use a raw InputStream to get all the data that is sent over the net.
and on the client side, i send a text file over like so: (in Android)
HttpClient httpClient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://my-hosted-url/postit"); MultipartEntity entity = new entity.addPart("myImageFile", new FileBody(someLogFile)); httpost.setEntity(entity); HttpResponse response; response = httpClient.execute(httpost); This works just fine... sort of. the problem is that when i try to view the file/data that is sent, it has a header on top of it, as such:
--NNqarc4FsG0G8hUzd82A6TCjgzKH Content-Disposition: form-data; name="myString" Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit STRING_VALUE ---NNqarc4FsG0G8hUzd82A6TCjgzKH Content-Disposition: form-data; name="myImageFile"; filename="something.txt" Content-Type: application/octet-stream Content-Transfer-Encoding: binary
[Thu Aug 14 17:14:26 PDT 2014] then the real log starts here...
How do i get rid of the headers that is somehow stuck to the body?