cancel
Showing results for 
Search instead for 
Did you mean: 

File Upload - Code Snippet

Former Member
0 Kudos

Hi Java Experts,

We have an application where in the user has to browse a file of type dbf,xls,txt,xml from the local system and upload into the temp path in server. I tried implementing but could write on to txt file and the other file formats get corrupted.

Can I have a common code snippet for reading the file from the client system and saving it on to the server for all the file types.

I am doing the File upload module in Portal Development Kit.

Kindly share me the code snippet if anybody have implemented in their applications?

Regards,

Ganesh N

Edited by: Ganesh Natarajan on May 22, 2009 10:53 AM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ganesh,

indeed we have a file upload in our application. It uses commons-fileupload for the parsing of the request.

It works with more than one file (if you need this).

Best regards, Jan

this is our (condensed) html:

<html>

<body>

<form method="post" enctype="multipart/form-data" action="/yourapp/uploadexample">

<input type="file" name="file1"><br>

<input type="file" name="file2"><br>

<input type="submit" value="send">

</form>

</body>

</html>

and this is our servlet:

import org.apache.commons.fileupload.*

...

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

boolean isMultipart = ServletFileUpload.isMultipartContent(req);

if (isMultipart) {

// Create a factory for disk-based file items

FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

try {

// Parse the request to get a list of FileItem

List items = upload.parseRequest(req);

// Process the uploaded items

int numOfContents = processUploadItems(items);

resp.getWriter().print("files: " + numOfContents);

} catch (Exception e) {

resp.getWriter().print(e);

}

}

}

private int processUploadItems(List items) throws Exception {

int numOfContents = 0;

if (!items.isEmpty()) {

// Process all the uploaded items

Iterator iter = items.iterator();

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (!item.isFormField()) {

String fileName = item.getName();

if (fileName != null && fileName.length() > 0) {

// Process the upload of one file

File uploadedFile = new File("d:
tmp
" + fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1));

item.write(uploadedFile);

numOfContents++;

}

}

}

}

return numOfContents;

}

Former Member
0 Kudos

Hi Jan,

Can u plz provide the jar files for importing org.apache.commons.fileupload. I downloaded from the following site

http://commons.apache.org/downloads/download_fileupload.cgi and added the jar files in lib folder.. bt stil the imports cannot be resolved..

So can u plz provide the jar files

Regards,

Sowmya

Answers (2)

Answers (2)

Former Member
0 Kudos

done

Former Member
0 Kudos

done