The problem I am having is that I can only upload images from the projects directory (/home/usr/workspace/project/~from here~).
For obvious reasons this won't work when I go to publish this feature. I am not sure where I should configure this differently. Help me stack overflow you're my only hope.
#RequestMapping("/saveImage")
public String getPreparedUploadUrl(#RequestParam File fileName,
HttpSession session) throws IOException, InterruptedException {
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(msec);
ObjectMetadata md = new ObjectMetadata();
md.setContentType("image/jpg");
md.setContentLength(fileName.length());
md.setHeader(fileName.getName(), fileName.getAbsolutePath());
File file = new File(fileName.getAbsolutePath());
FileInputStream fis = new FileInputStream(file);
byte[] content_bytes = IOUtils.toByteArray(fis);
String md5 = new
String(Base64.encodeBase64(DigestUtils.md5(content_bytes)));
md.setContentMD5(md5);
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest("wandering-wonderland-
images", fileName.getName());
generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
generatePresignedUrlRequest.setExpiration(expiration);
URL s =
s3client.generatePresignedUrl(generatePresignedUrlRequest);
try {
UploadObject(s, fileName);
} catch (IOException e) {
e.printStackTrace();
}
session.setAttribute("saved", fileName + " has been saved!");
return "redirect:/saved3";
}
// working, don't f#$# with it!
public static void UploadObject(URL url, File file) throws
IOException, InterruptedException {
HttpURLConnection connection=(HttpURLConnection)
url.openConnection();
InputStream inputStream = new
FileInputStream(file.getAbsolutePath());
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStream out =
connection.getOutputStream();
byte[] buf = new byte[1024];
int count;
int total = 0;
long fileSize = file.length();
while ((count =inputStream.read(buf)) != -1)
{
if (Thread.interrupted())
{
throw new InterruptedException();
}
out.write(buf, 0, count);
total += count;
int pctComplete = new Double(new Double(total) / new
Double(fileSize) * 100).intValue();
System.out.print("\r");
System.out.print(String.format("PCT Complete: %d",
pctComplete));
}
System.out.println();
out.close();
inputStream.close();
int responseCode = connection.getResponseCode();
System.out.println("Service returned response code " +
responseCode);
}
Related
I need to download a file from the following link in Java
[http://www.nseindia.com/content/historical/EQUITIES/2017/OCT/cm30OCT2017bhav.csv.zip][1]
I have the code written in C#, can some one suggest Java equivalent code
WebClient webClient = new WebClient();
String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
String agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1";
webClient.Headers.Add(HttpRequestHeader.Accept, accept);
webClient.Headers.Add(HttpRequestHeader.UserAgent, agent);
webClient.UseDefaultCredentials = true;
webClient.DownloadFile(source, target);
I myself found a solution
source = "http://www.bseindia.com/download/Bhavcopy/Derivative/bhavcopy07-11-17.zip";
target = "d:\Market Feeds\EQD BSE Bhavcopy\"
public static void downloadFileHttp(String source, String destination) throws Exception {
try{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipaddress, port));
URL oracle = new URL(source);
URLConnection yc = oracle.openConnection(proxy);
InputStream in = yc.getInputStream();
FileOutputStream out = new FileOutputStream(destination + "\\bhavcopy.zip");
copySource2Dest(in, out, 1024);
out.close();
extractFolder(destination + "\\bhavcopy.zip", destination);
//Path path = FileSystems.getDefault().getPath(destination, "bhavcopy.zip");
//boolean succ = Files.deleteIfExists(path);
System.out.println("Download is successfull");
}
catch(Exception e){
System.out.println("Error in downloading : " + e);
}
}
public static void copySource2Dest(InputStream input, OutputStream output, int bufferSize)
throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
public static void extractFolder(String zipFile,String extractFolder)
{
try
{
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = extractFolder;
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
ZipEntry entry;
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
zip.close();
}
catch (Exception e){
System.out.println("ERROR: "+e.getMessage());
}
}
I am downloading a PDF from a URL and saving it to my local drive.
The download code is working perfectly, the problem is that when I try to measure the size of the file it always claims it to be 52 bytes. I'm baffled... could you please review my code and tell me if I'am missing something?
try {
link = new URL("http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_" + entry[0] + "_2015.pdf");
// http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_BT_2015.pdf
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
} catch (Exception e) {
System.out.println("Couldn't retrieve : " + entry[1] + " " + year);
}
int bytes = fileName.length();
System.out.println(bytes);
Here. Just simply try this.
URL url = new URL("http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_" + entry[0] + "_2015.pdf");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
int size = conn.getContentLength();
if (size < 0) {
System.out.println("File not found");
} else {
System.out.println("File size in Bytes: " + size);
}
I want to upload an image to FTP Server. Currently i am using JDeveloper 12c(12.1.3.0).
My Code:
private static final int BUFFER_SIZE = 4096;
public String fileUploadMethod(String imagePath){
String ftpUrl = "ftp://";
String host = "http://192.168.0.42";
String user = "XXXXXX";
String pass = "XXXXXX";
String filePath = "783771-1.jpg";
String uploadPath = imagePath;
ftpUrl =ftpUrl + user +":"+ pass+"#"+host+"/"+filePath+";";
System.out.println("Upload URL: " + ftpUrl);
try {
URL url = new URL(ftpUrl);
URLConnection conn = url.openConnection();
OutputStream outputStream = conn.getOutputStream();
FileInputStream inputStream = new FileInputStream(uploadPath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("File uploaded");
return "File uploaded";
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
I am getting an error MalFormedURLException i.e. in detail message "unknown protocol:ftp"
Is there any other option to upload an image using JDeveloper.
Any idea regarding this.
Thanks, Siddharth
Your ftpUrl is wrong. Remove http:// in the host variable. Should be ok then
I haven't really tried ftp upload. But I had tried with multipart form upload. As far as I know, MAF doesnt provide Out-Of-Box support for file upload. What I did was essential recreating the HTTP stream for the image upload.
The POC code is attached below. This may be definitely the CRUDEST implementation but I am not sure if there is a better way.
public void doUpload() {
try {
DeviceManager dm = DeviceManagerFactory.getDeviceManager();
String imgData =
dm.getPicture(50, DeviceManager.CAMERA_DESTINATIONTYPE_FILE_URI, DeviceManager.CAMERA_SOURCETYPE_CAMERA,
false, DeviceManager.CAMERA_ENCODINGTYPE_PNG, 0, 0);
imgData = imgData.substring(7, imgData.length());
int start = imgData.lastIndexOf('/');
String fileName = imgData.substring(start+1, imgData.length());
RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
restServiceAdapter.clearRequestProperties();
String requestMethod = RestServiceAdapter.REQUEST_TYPE_POST;
String requestEndPoint = restServiceAdapter.getConnectionEndPoint("serverBaseUrl");
String requestURI = "/workers/100000018080264";
String request = requestEndPoint + requestURI;
HashMap httpHeadersValue = new HashMap();
httpHeadersValue.put("X-ANTICSRF", "TRUE");
httpHeadersValue.put("Connection", "Keep-Alive");
httpHeadersValue.put("content-type","multipart/form-data; boundary=----------------------------4abf1aa47e18");
// Get the connection
HttpConnection connection = restServiceAdapter.getHttpConnection(requestMethod, request, httpHeadersValue);
OutputStream os = connection.openOutputStream();
byte byteBuffer[] = new byte[50];
int len;
//String temp is appended before the image body
String temp = "------------------------------4abf1aa47e18\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" +fileName+ "\"\r\nContent-Type: image/jpeg\r\n\r\n";
InputStream stream = new ByteArrayInputStream(temp.getBytes("UTF-8"));
if (stream != null) {
while ((len = stream.read(byteBuffer)) >= 0) {
os.write(byteBuffer, 0, len);
}
stream.close();
}
FileInputStream in = new FileInputStream(imgData);
if (in != null) {
while ((len = in.read(byteBuffer)) >= 0) {
os.write(byteBuffer, 0, len);
}
in.close();
}
//The below String is appended after the image body
InputStream stream2 =new ByteArrayInputStream("\r\n------------------------------4abf1aa47e18--\r\n".getBytes("UTF-8"));
if (stream2 != null) {
while ((len = stream2.read(byteBuffer)) >= 0) {
os.write(byteBuffer, 0, len);
}
stream2.close();
}
int status = connection.getResponseCode();
InputStream inputStream = restServiceAdapter.getInputStream(connection);
ByteArrayOutputStream incomingBytes = new ByteArrayOutputStream() // get and process the response.
while ((len = inputStream.read(byteBuffer)) >= 0) {
incomingBytes.write(byteBuffer, 0, len);
}
String ret = incomingBytes.toString();
incomingBytes.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Please, help me. I need to get full file size and already writed in while loop. I need this to set progress of my progress bar.
This is my code:
try {
URL u = new URL(imgUrl);
InputStream is = u.openStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[1024];
int length;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "saved" + File.separator);
root.mkdirs();
String name = "" + System.currentTimeMillis() + ".jpg";
File sdImageMainDirectory = new File(root, name);
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
OutputStream output = new FileOutputStream(sdImageMainDirectory);
while ((length = dis.read(buffer))>0) {
output.write(buffer, 0, length);
}
} catch (MalformedURLException mue) {
Log.e("SYNC getUpdate", "malformed url error", mue);
} catch (IOException ioe) {
Log.e("SYNC getUpdate", "io error", ioe);
} catch (SecurityException se) {
Log.e("SYNC getUpdate", "security error", se);
}
If you want to get the number of bytes you already have written, use something like this:
Add a variable called writtenBytes before your while loop:
long writtenBytes = 0L;
Then, in your while loop, add the following code:
while ((length = dis.read(buffer))>0) {
output.write(buffer, 0, length);
writtenBytes += length;
}
To get the file size before downloading your file, you'll have to change your downloading code to something like:
URL url = new URL(imgUrl);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream inputStream = url.openStream();
DataInputStream dis = new DataInputStream(is);
I was trying to save a image from a link in a website I have written this code but this does not work ..plz help me to do this
public void imageshow(String linkText) {
try {
URL url = new URL(linkText);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4 * 1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream("C://chart.gif");
fos.write(response);
fos.close();
} catch (Exception e) {
}
}
Taken from here
import java.net.*;
import java.io.*;
public class DownloadFile
{
public static void main(String[] args)
{
try
{
/*
* Get a connection to the URL and start up
* a buffered reader.
*/
long startTime = System.currentTimeMillis();
System.out.println("Connecting to Mura site...\n");
URL url = new URL("http://www.getmura.com/currentversion/");
url.openConnection();
InputStream reader = url.openStream();
/*
* Setup a buffered file writer to write
* out what we read from the website.
*/
FileOutputStream writer = new FileOutputStream("C:/mura-newest.zip");
byte[] buffer = new byte[153600];
int totalBytesRead = 0;
int bytesRead = 0;
System.out.println("Reading ZIP file 150KB blocks at a time.\n");
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
totalBytesRead += bytesRead;
}
long endTime = System.currentTimeMillis();
System.out.println("Done. " + (new Integer(totalBytesRead).toString()) + " bytes read (" + (new Long(endTime - startTime).toString()) + " millseconds).\n");
writer.close();
reader.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}