Skip to content

Instantly share code, notes, and snippets.

@luankevinferreira
Last active April 13, 2024 22:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luankevinferreira/5221ea62e874a9b29d86b13a2637517b to your computer and use it in GitHub Desktop.
Save luankevinferreira/5221ea62e874a9b29d86b13a2637517b to your computer and use it in GitHub Desktop.
Upload a file using HTTP put in Java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class UploadFileMain {
public static void main(String[] args) {
URLConnection urlconnection = null;
try {
File file = new File("C:/file.txt");
URL url = new URL("http://server:8080/path/file.txt");
urlconnection = url.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);
if (urlconnection instanceof HttpURLConnection) {
((HttpURLConnection) urlconnection).setRequestMethod("PUT");
((HttpURLConnection) urlconnection).setRequestProperty("Content-type", "text/plain");
((HttpURLConnection) urlconnection).connect();
}
BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int i;
// read byte by byte until end of stream
while ((i = bis.read()) > 0) {
bos.write(i);
}
bis.close();
bos.close();
System.out.println(((HttpURLConnection) urlconnection).getResponseMessage());
} catch (Exception e) {
e.printStackTrace();
}
try {
InputStream inputStream;
int responseCode = ((HttpURLConnection) urlconnection).getResponseCode();
if ((responseCode >= 200) && (responseCode <= 202)) {
inputStream = ((HttpURLConnection) urlconnection).getInputStream();
int j;
while ((j = inputStream.read()) > 0) {
System.out.println(j);
}
} else {
inputStream = ((HttpURLConnection) urlconnection).getErrorStream();
}
((HttpURLConnection) urlconnection).disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@sadiksikder
Copy link

question reagrading upload file. your code seems that it cant upload what i actually want to upload?

@nandanathreya
Copy link

Thanks for this bud, But this doesn't work if you want to upload excel file. since you're treating everything as a string.
To upload Excel or CSV need to follow below code instead, where data is written as a byte array.

int i;
byte[] buffer = new byte[4096];
while ((i = bis.read(buffer)) > 0) {
bos.write(buffer, 0, i);
}

@MohamedA95
Copy link

Thanks a lot this is the only way that solved my problem, I tried many libs but none worked. THANKS, THANKS.

@riteshgurjar0909
Copy link

Thanks

@akhil1309
Copy link

the above code is giving me error 'bad request'

@ankitgangal
Copy link

ankitgangal commented Jun 19, 2020

This is great, thanks for helping! One quick comment, I think this code has a bug. BufferedInputStream documentation says read() function returns a -1 at the end of the stream.

So the code snippet uploading the file should update to -

    BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(imageFilePath)));

    int i;
    // read byte by byte until end of stream
    while ((i = bis.read()) > -1) {
        bos.write(i);
    }

Or else your code will stop when it reads a 0 in the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment