i am using oreilly multipartrequest class to upload multiple images, images are getting uploaded fine , but i want to know the name of all the images uploaded per request.
i am trying to print all filename using the below but it is printing the name of the last file only. I am trying to upload files 1.jpg , 2.jpg , 3.jpg , but it is printing only 3.jpg , what should i modify in the code to make it print all names.:
MultipartRequest m = new MultipartRequest(request, "C:/Users/;
Enumeration files = m.getFileNames();
while (files.hasMoreElements()) {
String name = (String) files.nextElement();
filename = m.getFilesystemName(name);
System.out.println(filename);
}
You can also list the files by making a file with the directory you wish to list.
File dir = new File(directory);
for (File f : dir.listFiles()){
System.out.print(f.getName());
}
Hope this helps
For more refernce check this link:
Oracle reference on File class
Related
I am using java nio to create the tmp file like below
Path tempFile = Files.createTempFile("student_records", ".csv");
Files.write(tempFile, fileContents);
File file = tempFile.toFile();
return new FileSystemResource(file);
but when file is created in temp directory , the file is not student_records.csv but student_records122334343443434.csv , i mean some random number is appending to the file , i do not want that random number , how to do that , and i want confirmation on if application exited, this tmp files deleted automatically or not ? can someone please help on this
This is intended behaviour of Files.createTempFile().
If you want a file that you can explicitly set the name of that does not exist after the application is closed, you can use:
File file = new File("student_records.csv");
file.deleteOnExit();
This will (as the name suggests) delete the file when the program exits.
I'm using java Jclouds to upload to a container inside an OpenStack Swift, the upload is going fine on the root, but when i pass a path(contains folders then the file), the file is uploaded but also creates another folder with the same name of the file. the original file name is 8mb.bin
the code is:
try {
ByteSource fileBytes = Files.asByteSource(file);
File file = new File(filePath);
String name = "test/test2/" + file.getName();
Blob blob = blobStore.blobBuilder(name)
.userMetadata(ImmutableMap.of("ContentType", contentType, "test", String.valueOf(test)))
.payload(fileBytes)
.contentLength(file.length())
.contentType(contentType)
.build();
///sednig the request
blobStore.putBlob(ContainerName, blob, multipart());
return contentLength;
}
and in the designated path its like this:
the folder 8mb.bin has the path inside it /slo/1522766773.076000/8200000/33554432 and then a file called 00000000 with the same size of the original file size.
how to solve this?
Thanks
jclouds implements Swift multipart using Static Large Objects. This has the limitation that parts exist in the same namespace as the manifest and modifying or deleting the parts invalidates the manifest. JCLOUDS-1285 suggests putting the parts in another container to clean up object listing although this requires some extra logic for deletes and overwrites.
I'm trying to create a java program that will create and upload excel files into a google drive. After uploading I need it to give permissions. I have done all thatbut the problem lies in trying to convert the excel files into a google files in order that people who use the sheets are able to update them with their phones. I have not been able to find any help on how to do this on the google api. Any help will be appreciated.
Assuming you have the path to your Excel file:
java.io.File fileContent = new java.io.File(excelfilepath);
FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("application/vnd.ms-excel");
// THIS IS THE IMPORTANT PART
Insert request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();
// END OF THE IMPORTANT PART
if (file != null) {
showToast("Data Uploaded: " + file.getTitle());
}
I've been trying to save a plain text file into a specific folder in Google Drive on Android.
So far using the Documentation and QuickStart Guide on Google Drive I have been able to do a few things that are going in the right direction, first I was able to create a plain text file:
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, textContent).execute();
I have been able to create a new folder in the base directory of Google Drive with:
File body = new File();
body.setTitle("Air Note");
body.setMimeType("application/vnd.google-apps.folder");
File file = service.files().insert(body).execute();
I have also been able to list all of the folders in the user's Google Drive account with:
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
However I am a bit stuck on how to save a text file into a folder within Google Drive.
You need to use the parent parameter to put a file in a folder using insert. More details at https://developers.google.com/drive/v2/reference/files/insert
Something like this
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();
If you want to INSERT a file in specific folder in Google Drive then follow these steps. Lets assume that we have retrieved all folder from the Drive and now i will INSERT an empty file in the first folder from the list, So
//Getting Folders from the DRIVE
List<File> files = mService.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
File f =files.get(1)//getting first file from the folder list
body.setTitle("MyEmptyFile");
body.setMimeType("image/jpeg");
body.setParents(Arrays.asList(new ParentReference().setId(f.getId())));
com.google.api.services.drive.model.File file = mService.files().insert(body).execute();
Now this will create an empty file in the Folder which are at the top in the retrieve file list.
Step1 : Create a folder
File body1 = new File();
body1.setTitle("cloudbox");
body1.setMimeType("application/vnd.google-apps.folder");
File file1 = service.files().insert(body1).execute();
Step2 : Insert your file
File body2 = new File();
body2.setTitle(fileContent.getName());
body2.setMimeType("text/plain");
body2.setParents(Arrays.asList(new ParentReference().setId(file1.getId())));
File file2 = service.files().insert(body2, mediaContent).execute();
I am writting the code to upload a file using Spring's MultipartFile on server for that I have written following code
if(!partnersContentBean.getFile().isEmpty()){
MultipartFile file = partnersContentBean.getFile();
if(file.getOriginalFilename().endsWith(".jpeg")||file.getOriginalFilename().endsWith(".jpg")|| file.getOriginalFilename().endsWith(".gif")){
File dirPath = new File("//125.22.60.37/image/dev/cmt/");
if (!dirPath.exists()) {
dirPath.mkdirs();
}
URL url = new URL("http://125.22.60.37/image/dev/cmt/");
File destination = new File(url.toString());
file.transferTo(destination);
String url1 = request.getContextPath() + ApplicationConstants.imageUploadDirectory + file.getOriginalFilename();
System.out.println(url.getPath());
partnersContentBean.setPartnerImagename(file.getOriginalFilename());
partnersContentBean.setPartnerImagepath(destination.getPath());
}else
{
userModuleDetailBean.put("errorMessage", "File should be in type of jpg,Jpeg or GIF");
return new ModelAndView(new RedirectView("partnersAdd_CMT.htm"),"userModuleDetailBean",userModuleDetailBean);
}
}
but when I upload a file I get following exception java.io.FileNotFoundException: http:\125.22.60.37\image\dev\cmt (The filename, directory name, or volume label syntax is incorrect) dont know what path should i give to upload it
It looks like you're trying to transfer the uploaded file to another remote server (125.22.60.37). You can't do that - you can't represent an HTTP URL using a File object.
FileUpload is for storing the uploaded files to your local machine. Once there, you can worry about moving them to another remote server, but the two tasks are separate.