I want to upload a file in static/css/question_image which is a directory in my Spring Boot project.
By using below code i am able to upload the image but it is getting stored in my local system some directory and it doesn't appear in project folder structure.
Iterator<String> itr =multipartHttpServletRequest.getFileNames();
while(itr.hasNext()){
fileName = itr.next();
MultipartFile file =multipartHttpServletRequest.getFile(fileName);
System.out.println("File name is "+file.getOriginalFilename());
byte[] byteArr = file.getBytes();
File convFile = new File("static/"+file.getOriginalFilename());
convFile.createNewFile();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(convFile);
stream.write(byteArr);
stream.close();
I'm guessing your file is in the "static" sub-directory of the current directory, which, possibly, is the directory containing your JAR. You can't write back into the "project folder structure", as that appears to be in the JAR.
I suggest you rethink what you are intending to do with the output and where it needs to go.
Related
I want to write into a json file inside the resource folder of springboot. So I wanted to check, how to create a file and insert data. If file exists after creation then add the data into file, else create file and add data.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
FileOutputStream fileOutputStream = new FileOutputStream(new
File(classLoader.getResourceAsStream("/Response.json").toString()));
OutputStreamWriter outputStreamWriter = new
OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
try {
for(list){}
string result="user Should be active user";
writer.write(String.valueOf(result));
writer.close();
} catch{}
IMHO, If you're using docker or any container to deploy the apps, just let the code to write on the root folder (home where app.jar deployed), coz in the runtime actually the metadata will not accessible since it already bundled on the jar by spring-boot.
When I run my war file on Tomcat server then I run my project on chrome and download the xls file from my project and this file showing in tomcat bin folder as well as download folder in our computer.
Please suggest me how we can stop this download file in tomcat bin folder
thanks
String FILE_EXTENSION = ".xlsx";
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
filename = "SearchPayment_Transactions_" + df.format(new Date()) + FILE_EXTENSION;
File file = new File(filename);
// this Writes the workbook
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.flush();
out.close();
wb.dispose();
fileInputStream = new FileInputStream(file);
addActionMessage(filename + " written successfully on disk.");
i think the this problem can be sovled, just by fixing the place you want to created the file
String FILE_EXTENSION = ".xlsx";
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
filename = "SearchPayment_Transactions_" + df.format(new Date()) + FILE_EXTENSION;
File file = new File(path any fixed directory like temp\filename);
As long as you specify the path where you want to generate the file then it will generating only in tht directory. PLease make proper permission is given to path to generate file, and this will solve your issue.
The file appears twice on your computer, because your servlet code saves the *.xlsx file to disk before sending it to your browser. That's the behavior your chose in your code.
Remark however, that file in your code is a relative path, so the folder you write it is the working directory (according to the OS) of your server. The value of the working directory is not defined in the Servlet Specification and may vary from system to system.
A better solution would be:
either don't write any file at all and write your data directly to ServletResponse#getOutputStream(),
or write the file to the Servlet's temporary directory, which you can obtain through (File) servletContext.getAttribute(ServletContext.TEMPDIR). E.g. you can replace your file variable with:
final File file = new File((File) servletContext.getAttribute(ServletContext.TEMPDIR), filename);
I am trying to get the basics of I/O with Spring.
I am working through the File Upload samples. I want to store the files in a temporary folder in the project for testing.
I have this code:
#RequestMapping(value="/upload", method=RequestMethod.POST)
public #ResponseBody String handleFileUpload(#RequestParam("name") String name,
#RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
//get directory
stream.write(bytes);
stream.close();
I have created folder "files" within the project files. I want to know how to target the local folder.
I have tried using class File's alternate constructor.
The only way to write files in external(from web container) directory is to use the full direct path to the file. Just use the full file path in following File constructor.
File(String pathname)
Creates a new File instance by converting the given pathname string
into an abstract pathname.
My require file in sd card has path
Android/data/mypakagename/android/myfile/abc/myfile.txt
i have a url from where i will download this myFile.txt and place it in this location.
Right now I have directory Android/data/mypakagename/ created in sdCard.
Now i am using following code.
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
file.mkdirs();
This creates all folders however it also create myfile.txt as a folder.I want to create all missing directories except last one. How can i do that?
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
File parent = file.getParentFile();
parent.mkdirs();
String filename = "myfile.txt"
File dir = new File ("Android/data/mypakagename/android/myfile/abc");
dir.mkdirs();
File file = new File(dir, filename);
I am trying to zip the following file structure on my machine,
parent/
parent/test1
parent/test1/image1.jpeg
parent/test2
The problem here is i cant zip the above file structure using java. I have google and found following code sample but it only zip the files only inside a given folder.
File inFolder=new File("out");
File outFolder=new File("Out.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(new FileOutputStream(outFolder)));
BufferedInputStream in = null;
byte[] data = new byte[1000];
String files[] = inFolder.list();
for (int i=0; i<files.length; i++)
{
in = new BufferedInputStream(new FileInputStream
(inFolder.getPath() + "/" + files[i]), 1000);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while((count = in.read(data,0,1000)) != -1)
{
out.write(data, 0, count);
}
out.closeEntry();
}
out.flush();
out.close();
In the above code the out is a folder and we need to have some files..also folder cannot be empty if so it throws a exception java.util.zip.ZipException or cant contain any sub folders even files inside it (eg:out\newfolder\image.jpeg) if so it throws a java.io.FileNotFoundException: out\newfolder (Access is denied).
In my case im costructig the above file structure by quering the database sometime empty folders along the folder structure can be have.
Can some one please tell me a solution?
Thank You.
What is probably happening is that you're trying to treat every entry as a FileInputStream. However, for a directory, this is not true. Since the path is not to a file, when you try to read it, a FileNotFoundException is thrown. For directories, you still want to create the ZipEntry, but instead of trying to read in any data, just skip it and move on to the next path.
write two methods. The first one takes dirpath, makes a zip stream and calls another method which copies files to the zip stream and calls itself recursively for directories as below:
open an entry in the zip stream for the given directory
list files and dirs in the given directory, loop through them
if an entry is a file, open an entry, copy file content to the entry, close it
if an entry is a directory, call this method. Pass the zip stream
close the entry.
The first method closes the zip stream.