0

I need to upload .dat file from jsp page. I am using struts. I am using

<input type="file" name="file"  size=25 />

in jsp and in action class

FileUploadForm uploadForm = (FileUploadForm) form;
FormFile file = uploadForm.getFile();
InputStream stream = file.getInputStream();

After this I am confused. I dont want to open and read the dat file as the size of the may be big. I just wanna create/copy the same dat file in some specified path in server as per the original name of the dat file. How to do it?

And if there is no other way but read it then also tell me how read and write it to dat file.

1 Answer 1

1

These are the tasks you need to complete in order to finish this task:

  1. Decide where uploaded files are to be stored on the server
  2. Open an output stream to a file in the appropriate location
  3. Copy from the uploaded data from it's input stream to the output stream

Here is some pseudo-code, utilizing Apache IOUtils to copy the stream data:

final FormFile formFile = uploadForm.getFile();
final String outPath = "/somerootpath/" + formFile.getFileName();
final OutputStream outStream = new FileOutputStream(outPath);
IOUtils.copy(formFile.getInputStream(), outStream);
IOUtils.closeQuietly(outStream);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the answer. But compiler error is coming and saying "The method IOUtils.copy(InputStream, OutputStream) is undefined for the type IOUtils" and same for IOUtils.closeQuietly(outStream);
To use Apache IOUtils you need to download it and include the JAR in your build path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.