Last active
April 13, 2024 22:54
-
-
Save luankevinferreira/5221ea62e874a9b29d86b13a2637517b to your computer and use it in GitHub Desktop.
Upload a file using HTTP put in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Thanks
the above code is giving me error 'bad request'
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
Thanks a lot this is the only way that solved my problem, I tried many libs but none worked. THANKS, THANKS.