Sweet Dream
Sweet Dream

Reputation: 245

How to upload dat file using JSP, Java (Struts.)?

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.

Upvotes: 0

Views: 904

Answers (1)

Perception
Perception

Reputation: 80593

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);

Upvotes: 1

Related Questions