How upload image in my root folder? - java

I have successfully manage to upload my images to c:/images
How can I upload the in my root folder?
String fileName = getFileName(filePart);
FileOutputStream os = new FileOutputStream("C:/images" + fileName);
os.write(b);

You can use ServletContext.getRealPath(relativePath) to obtain the real path of file.
String relativePath="/images/" + fileName;
String realPath=getServletContext().getRealPath(relativePath);
FileOutputStream stream=new FileOutputStream(realPath);

Related

How to get URL of Vaadin resources path?

I use Eclipse + Vaadin framework.
At first I save my image into the WEB-INF folder.
Then I load this image into the FileResource variable, create Image variable and then add Image variable into Layout.
The page URL to the image resource looks like: http://servername/sitename/APP/connector/0/16/source/qwerty1.png
How to get URL in that format for use image externally?
Variable basepath returned local path: "..../WEB-INF/qwerty1.png"
String str = (String) VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
File temp = new File(str);
FileOutputStream fos = new FileOutputStream(temp);
fos.write(os.toByteArray());
fos.flush();
fos.close();
String basepath = VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
FileResource resource = new FileResource(new File(basepath));
Image image2 = new Image("Image from file", resource);
addComponent(image2);
If you put the file in ...src/main/webapp/VAADIN/image.png, it should be available using for example localhost:8080/VAADIN/image.png.

Save to desktop without the exact path

I want to save a file in my desktop. So I have
FileOutputStream out = new FileOutputStream(new File("C:\\path_to_Dekstop\\print.xls"));
and it works. But I want to save the file without put the exact path to the desktop. I searched it and I found similar questions and I came up with this solution:
File desktopDir = new File(System.getProperty("user.home"), "Desktop");
System.out.println(desktopDir.getPath() + " " + desktopDir.exists());
String pathToDesktop = desktopDir.getPath();
FileOutputStream out = new FileOutputStream(new File(pathToDesktop));
but I got an error
java.io.FileNotFoundException: C:\Users\nat\Desktop (Access is denied)
pathToDesktop represents the directory of the Desktop, you should supply a file name to write to
FileOutputStream out = new FileOutputStream(new File(desktopDir, "File to be written to"));
Which will place the "File to be written to" on the desktop
You can't write directly to Desktop as its a folder but not a file. You need to write to a file. DO something like thi s:-
File desktopDir = new File(System.getProperty("user.home"), "Desktop");
System.out.println(desktopDir.getPath() + " " + desktopDir.exists());
String pathToDesktop = desktopDir.getPath();
FileOutputStream out = new FileOutputStream(new File(pathToDesktop+System.getProperty("file.separator")+"print.xls"));
This will write to print.xls in Desktop.

How to upload file to Dropbox?

I want to upload a file to drop-box using a web-application. But the problem is that Java is asking the complete file path. What do I have to do? This is my code:
File inputFile = new File("D://New Text Document.txt");
System.out.println("inputFile.getAbsoluteFile(): " + inputFile);
FileInputStream inputStream = new FileInputStream("D://New Text Document.txt");
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} finally {
inputStream.close();
}
In the first line it is asking for the file path; how is this possible?
you can use relative path
File inputFile = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} finally {
inputStream.close();
}
more details here
The Dropbox Java Core SDK tutorial has the following code as a sample for uploading a file:
File inputFile = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
try {
DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream);
System.out.println("Uploaded: " + uploadedFile.toString());
} finally {
inputStream.close();
}
In the sample above, in the first line, "working-draft.txt" is the local path to a local file. This needs to point to an existing local file. Your code has "D://New Text Document.txt" so you should first make sure there is actually a file there.
In the fourth line, "/magnum-opus.txt", the first parameter passed to uploadFile, is the desired remote path, that is, where in the Dropbox folder you want to upload the file to. The API requires remote paths be referenced relatively like this. The uploadFile method is documented here.

Create zip file to local machine

I have a below code in which my zip file is getting created on the server machine, i want the zip file to be created in the local machine, below is my code, please check the below code and let me know if anybody has a solution for it .
<%!
public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
%>
<%
String imgID = request.getParameter("iID").toString();
String epsFile = request.getParameter("epsNm").toString();
String ZipFile = imgID + ".zip";
//FileOutputStream fos = new FileOutputStream("d:/" + ZipFile);
FileOutputStream fos = new FileOutputStream(ZipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
File temp = new File(imgID);
String absolutePath = temp.getAbsolutePath();
System.out.println("filepath" + absolutePath);
String relativeWebPath = "CoverCapPDF/"+ imgID;
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
System.out.println("absoluteDiskPath" + absoluteDiskPath);
String relativeWebPathEPS = "eps/"+ epsFile;
String absoluteDiskPathEPS = getServletContext().getRealPath(relativeWebPathEPS);
System.out.println("absoluteDiskPath" + absoluteDiskPathEPS);
String file1Name = absoluteDiskPath;
String file2Name = absoluteDiskPathEPS;
String file3Name = "file2.txt";
addToZipFile(file1Name, zos);
addToZipFile(file2Name, zos);
zos.close();
fos.close();
%>
please help me :)
I am assuming you are dealing with a web application which uses JSP (since the above syntax suggests the same). The answer then is you cannot.
What you can do is
Create the file at the server and ask the client to download refer here
Create an applet and the applet can have the zip code (though not recommended for security reasons)
First of all, you should not use a JSP for this, but a servlet. JSPs are view components, whose role is to generate HTML markup using the JSP EL, the JSTL and other custom tags, but no scriptlet.
Second: you're writing to a FileOutputStream. that obviously writes your zip entries to a file. You want to write your zip entries to the HTTP response. You should thus use the response output stream to write your zip entries.
To tell the browser that you're sending what should be saved as a zip file, use
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
(this should be called before sending anything to the response output stream)

Create pdf through java.io

I tried creating a pdf file out of another one(in my local drive) using java.io. The thing is a file with a .pdf extension got created but im unable to open the file, it says the file is already in use and most importantly the size of the file is too large and it keeps on increasing (origin file size : 5,777kB and the newly created one file size as of now is 38,567kB). Im not that much of skilled java programmer but still i would appreciate if anyone can give me an explanation ..
String path = "D:\\priya_Docs\\Android pdfs\\Professional_Android_Application_Development.pdf";
File file = new File(path);
System.out.println("Located a file " + file.isFile());
String filesArray = file.getPath();
File getFile = file.getAbsoluteFile();
FileInputStream fis = new FileInputStream(getFile);
FileOutputStream fos = new FileOutputStream(
"D:\\priya_Docs\\Androiddoc.pdf");
for (int b = fis.read(); b != -1;) {
fos.write(b);
}
Simple use,
FileUtils.copyFile()
you meet the two problems
first,you have to close the resource: fis and fos,or it will say the file already in use
second,you have to use the byte[] to receive the data because pdf file is organized in byte arrays
String path = "D:\\priya_Docs\\Android pdfs\\Professional_Android_Application_Development.pdf";
File file = new File(path);
System.out.println("Located a file " + file.isFile());
String filesArray = file.getPath();
File getFile = file.getAbsoluteFile();
FileInputStream fis = new FileInputStream(getFile);
FileOutputStream fos = new FileOutputStream(
"D:\\priya_Docs\\Androiddoc.pdf");
byte[] buff=new byte[1024];
int len;
while((len=fis.read(buff))>=0) {
fos.write(buff,0,len);
}
fis.close();
fos.close();

Categories