i uploaded an image from client to server...i need to save the server path in database...
i saved the image sucesssfully..i have a problem in path
I need to save the path like this in database
https://localhost/...../filename
but in mine the path was taken like this
d:/...../filename
this is my code
String root = request.getServletContext().getRealPath("/");
File path = new File(root + "/images/");
if (!path.exists())
{
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + vehicleId +".png");
String pathString=uploadedFile.getAbsolutePath();
item.write(uploadedFile);
Please sort out my problem..
thanking you..
Related
I need to insert a file to a path. However, the file name need to change to a specific name before inserted.
How can I change the name of the file before insert to path? As many resources online only able to change the file name after inserted. Online Resource for rename file
My code currently
String localPath = "c://Users/foody/Documents/write_file_local/";
String finalPath = localPath + file.getOriginalFilename();
File uploadPath = new File(finalPath);
if (!uploadPath.getParentFile().exists()) {
uploadPath.getParentFile().mkdirs();
}
//I think need to rename the file here before insert to path
byte[] bytes = file.getBytes();
Path path = Paths.get(finalPath);
Files.write(path, bytes);
Replace your code with this and update your "CustomName_ABC" with your new fileName.
String localPath = "c://Users/foody/Documents/write_file_local/";
String finalPath = localPath + "CustomName_ABC";
File uploadPath = new File(finalPath);
if (!uploadPath.getParentFile().exists()) {
uploadPath.getParentFile().mkdirs();
}
Files.copy(file.toPath(), uploadPath.toPath());
You can copy your old file to a new filePath (directory) by using Files.copy() method. It will take two parameters:
Old file path
New file path
I want to upload image, and return filepath to save in database so i can later diplay that image.
I am using Primefaces UploadeFile. But when i try to upload file, i get file not found Exception. I am propably making error in path, but i don't know where those files are saved.
I have method that performs file upload.
Essentialy i generate unique file name and try to copy it to location.
Date date = new Date();
String filename = author.getSlug() + "-" + date.getTime() + "." + FilenameUtils.getExtension(image.getFileName());
File path = new File("/uploads/authors", filename);
InputStream input = image.getInputstream();
Files.copy(input, path.toPath(), StandardCopyOption.REPLACE_EXISTING);
This code throws file not found exception.
I have taken screen shots of appium test with following script.
String path;
try {
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
path = "/Users/admin/Desktop/newfolder" + source.getName();
org.apache.commons.io.FileUtils.copyFile(source, new File(path));
}
catch(IOException e) {
path = "Failed to capture screenshot: " + e.getMessage();
}
I want to take them to a folder named with timestamp.
But now I'm getting them to desktop named like this
How to give path to a folder to save these screen shots during appium test?
Try this to create folder with timestamp name:
String.valueOf(new Timestamp(System.currentTimeMillis())).replace(":", "-")
Add a "/" after "newfolder" when you set path = to make it
path = "/Users/admin/Desktop/newfolder/" + source.getName();
Taking The N…'s answer into account, the path should be created like:
String timestampAsString = String.valueOf(new Timestamp(System.currentTimeMillis())).replace(":", "-");
path = "/Users/admin/Desktop/" + timestampAsString + "/" + source.getName();
there is folder temp to which the files uploaded by users are stored. the file name is same for each user but the content is different. Each user uploads a file called abc.xlsx. now when "A" user uploads abc.xlsx file after processing that file should be deleted. But currently i am deleting all the files in the folder. which is a problem since one more user might be uploading the file ehich will be cleared too. So i was thinking of renaming the file by appending the username to the file and then delete that particular file.
This is the file upload:
ProcessForm uploadForm = (ProcessForm)form;
String folderpath = "servers/temp";
String filePath = folderpath + "/" + uploadForm.getUploadedFile().getFileName();
This will delete all the files in the folder:
String tempPath = folderpath;
File file = new File(tempPath);
File[] files = file.listFiles();
for (File f:files)
{
if (f.isFile() && f.exists())
{
f.delete();
}
}
I think i got it. This is working as expected:
String folderpath = "servers/temp";
String filePath = folderpath + "/" + "abc_"+user.getUsername()+".xlsx";
outputStream = new FileOutputStream(new File(filePath));
outputStream.write(uploadForm.getUploadedFile().getFileData());
Code to delete file:
File file = new File(filePath);
boolean fileDelete = file.delete();
if (fileDelete)
{
mLogger.debug("successfully deleted");
} else {
mLogger.error("cant delete a file");
}
I'm using the following code to save an image to a folder when I select an option:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String filename = "teste.png";
File file = new File(path, filename);
filename = file.toString();
Highgui.imwrite(filename, mRgba);
But i'd like the saved image to NOT OVERWRITE the image that's already in the folder. How could I do that? Using a kind of index for each image or something like that, I think, but how?
Thanks.
Maybe something like this?
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyAppDir");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "testimage_" + timeStamp + ".png");