Skip to content

Instantly share code, notes, and snippets.

@veysiertekin
Created April 3, 2014 21:35
Show Gist options
  • Save veysiertekin/d7bb17806d639e8e7d6e to your computer and use it in GitHub Desktop.
Save veysiertekin/d7bb17806d639e8e7d6e to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.log4j.Logger;
public class FileUtil {
private static final Logger logger = Logger.getLogger(FileUtil.class);
public static void saveAs(byte[] bytes, String fileLocation) {
try {
FileOutputStream fos = new FileOutputStream(new File(fileLocation));
fos.write(bytes);
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] download(String fileURL) {
HttpURLConnection httpConn = null;
try {
ByteArrayOutputStream outputStream = null;
byte[] bytes = null;
URL url = new URL(fileURL);
httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
}
else {
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
logger.error("Content-Type = " + contentType);
logger.error("Content-Disposition = " + disposition);
logger.error("Content-Length = " + contentLength);
logger.error("fileName = " + fileName);
InputStream inputStream = httpConn.getInputStream();
outputStream = new ByteArrayOutputStream();
int bytesRead = -1;
byte[] buffer = new byte[1024];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
logger.error("File downloaded");
bytes = outputStream.toByteArray();
}
else {
InputStream stream = httpConn.getErrorStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
String response = new String(buffer.toByteArray());
logger.error("No file to download. Server replied HTTP code: " + responseCode + "\n" + response);
}
return bytes;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
finally {
if (httpConn != null)
httpConn.disconnect();
}
}
public static File createTempDirFile(String fileName) {
return new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + fileName);
}
public static byte[] bytes(String fileName) throws IOException {
return bytes(new File(fileName));
}
public static byte[] bytes(File file) throws IOException {
int size = (int) file.length();
byte[] fileContent = new byte[size];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int read = 0;
int numRead = 0;
while (read < fileContent.length && (numRead = dis.read(fileContent, read, fileContent.length - read)) >= 0) {
read = read + numRead;
}
dis.close();
return fileContent;
}
public static byte[] bytes(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment