-
-
Save luankevinferreira/5221ea62e874a9b29d86b13a2637517b to your computer and use it in GitHub Desktop.
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 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);
}
Thanks a lot this is the only way that solved my problem, I tried many libs but none worked. THANKS, THANKS.
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.
question reagrading upload file. your code seems that it cant upload what i actually want to upload?