I have writing a program which ask for video file from user. once video is browser by user it is save inside war/content. I do not have any problem to do this. when i want to see the video, i used video tag of html and give the relative path. video come perfectly fine and it can be viewed.
I used the following line of code to resolve the path.
System.setProperty("webapp.root", "C:/solution/totalsolution");
File webappDir = new File(System.getProperty("webapp.root", "./"));
File webappInfo = new File(webappDir, "war");
if(!webappInfo.exists()){
File dir = new File(webappDir, "war");
if(!dir.exists()){
if(dir.mkdir()){
webappDir = dir;
}
}
}
webappDir = webappInfo;
webappInfo = new File(webappDir, "content");
if(!webappInfo.exists()){
File dir = new File(webappDir, "content");
if(!dir.exists() && dir.mkdir()){
webappDir = dir;
}
}
webappDir = webappInfo;
if(contentDir != null){
webappInfo = new File(webappDir, contentDir);
if(!webappInfo.exists()){
File dir = new File(webappDir, contentDir);
if(!dir.exists() && dir.mkdir()){
webappDir = dir;
}
}
webappDir = webappInfo;
}
this program create new folder in specified path like "C:/solution/totalsolution/war/content/abcd/xysa.mp4"
when i show any resource then i make relative path like this
http://localhost:8080/solution/content/abcd/xysa.mp4
till now every thing is ok.But when i extract war file and deploye it into server.
Then is way failed due to static location in system property.
So can any body tell be the right approch to save data into war folder and right back reading it on local as well as on server to.
1) Do not store it in the war folder but in a separate location (probably designed specifically for this).
2) When getting the file from the user, you need to generate new unique name for this file, and store somewhere (e.g. DB) the mapping from the original file name to the new file name. Then on the disk you store the file under the new name. This is to guarantee your file names are unique (think what happens if two users upload files with the same name).
Don't try to save data into your webapp; there is no guarantee that this directory will be writable or even unpacked from the jar. Instead, read a property that tells you where to store data saved at runtime.
Related
I have a list of files in a Google Bucket. I have to create a folder in Google Drive and upload all files into that folder from the Google Bucket. I am uploading the files correctly, but I am not able to upload the files into a specific folder. One by one the files are uploaded in the Drive root folder, but I want them in a particular folder.
Page<Blob> blobs = bucket.list(Storage.BlobListOption.prefix(BucketUrl));
Drive driveService = DriveConfUtil.getDriveService(accessToken);
for (Blob fileBlob: blobs.getValues()) {
InputStream inputStream = new
ByteArrayInputStream(fileBlob.getContent(BlobSourceOption.generationMatch()));
File file = new File();
file.setName(fileBlob.getName());
file.setMimeType("application/mydocfolder.folder");
File file1 = null;
try {
AbstractInputStreamContent streamContent = null;
streamContent = new InputStreamContent("application/pdf", inputStream);
file1 = driveService.files().create(file, streamContent).setFields("id").execute();
} catch (IOException e) {
e.printStackTrace();
}
}
I get that your script uploads the file correctly, but it does so in the Drive main folder instead of your desired folder. You can fix that easily just by filling the parents[] parameter in the request body. To specify the folder in that parameter you would need its id. Please keep in mind that a Drive file can be hosted on different folders at the same time. Don't hesitate to ask me for any clarification.
I tried this link:
How to create text file and insert data to that file on Android
But, it says "No such file or directory". Can anybody help me please? Thanks in advance!
---- Updated answer----
I think you can't create a directory inside the internal storage of the device. Except you've a root access for the app. You can only create the directory inside your app private folder within the following path String path = getFilesDir().
you can use like this below -
File mydir = context.getDir("mydirectory", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
getDir(StringName, int mode) method to create or access directories in internal storage.
/storage/emulated/0/Notes/ will always return No such file or directory except device is rooted and dir.mkDirs() will always return false for this path.
Hope this will help you.
I have create folder (i.e uploads ) in web application. I want to create one more folder inside "uploads" folder at runtime depends one the username of user. for this i have write below code. This code is creating folder and file but the location is different that i expected.
the location that i am getting is in eclipse location not web application location
D:\PAST\RequiredPlugins\JUNO\eclipse\uploads\datto\adhar.PNG
then i am getting error in FileOutStream that "system can't find the location specified."
public String getFolderName(String folderName, MultipartFile uploadPhoto)
throws ShareMeException {
File uploadfFile = null;
try {
File file = new File("uploads\\" + folderName);
if (!file.exists()) {
file.mkdir();
}
uploadfFile = new File(file.getAbsoluteFile()
+ "\\"+uploadPhoto.getOriginalFilename());
if (uploadfFile.exists()) {
throw new ShareMeException(
"file already exist please rename it");
} else {
uploadfFile.createNewFile();
FileOutputStream fout = new FileOutputStream(uploadfFile);
fout.write(uploadPhoto.getBytes());
fout.flush();
fout.close();
}
} catch (IOException e) {
throw new ShareMeException(e.getMessage());
}
return uploadfFile.getAbsolutePath();
}
i want to save uploaded file in web app "uploads" folder
Your filename is not absolute: uploads\folderName is resolved against the current directory, which the Eclipse launcher sets to JUNO\eclipse.
You should introduce an application variable like APP_HOME and resolve any data directory (including upload) against this variable.
Also, I suggest not to name anything (neither files nor directories) on your filesystem after user-entered input: you are asking for troubles (unicode characters in the user name) and especially security holes (even in combination with the unicode thing). If you really want to use the filesystem, keep the filename anonymous (1.data, 2.data, ...) and keep metadata inside some database.
You can do something on below lines in your webapp:-
String folderPath= request.getServletContext().getRealPath("/");
File file = new File (folderPath+"upload");
file.mkdir();
I want to make a program that you can email to someone and they can run it.
Right now my code for making a file is like this:
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();
But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?
Leave the path off entirely, it will use the directory of the project.
Change
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");
To
File f = new File("axmlfile.xml");
I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.
try{
//create a temporary file
File temp = File.createTempFile("axmlfile", ".xml");
System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}
I am stuck with an issue with reading .xlsx file. Some temporary files with random name are created under /tmp/poifiles directory whenever I use WorkbookFactory.create(inputStream);. This directory is created with RW-R-R- permission for the first user. So another user on the same machine when tries to access these files, he CANNOT.
Please suggest me any way
1) How can I create these temp files under /tmp directory and not always in /tmp/poifiles (I am using RHEL V5.0)
2) and how can I configure POI such as to change the location from where it reads the temporary files??
Anymore help to solve my problem of different users accessing same .xlsx files through POI is badly needed.
Yuppie...I got the solution....
POI uses the following method to create temp files.
public static File createTempFile(String prefix, String suffix)
{
if (dir == null) {
dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
dir.mkdir();
if (System.getProperty("poi.keep.tmp.files") == null) {
dir.deleteOnExit();
}
}
File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
if (System.getProperty("poi.keep.tmp.files") == null) {
newFile.deleteOnExit();
}
return newFile;
}
Now here as we can see it gets the location from property "java.io.tmpdir" and creates poifiles directory inside that...
I changed the location of java.io.tmpdir by setting this property (using System.setProperty("java.io.tmpdir", "somepath"))to user specific location..and Voila....Every user now can create temp files at location always accessible to them and not only the first user gets the privilege to create directory accessible only to him ...!!!
Here is how you can change the location from where POI reads the temporary files programmatically if you are not able to change system property "java.io.tmpdir"
File dir = new File("somepath");
dir.mkdir();
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(dir));
This is driven by the Apache POI TempFile and DefaultTempFileCreationStrategy helper classes.