on 05-22-2009 8:52 AM
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
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
done
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
done
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.