Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > upload excel and pdf file using Commons FileUpload package

Reply
Thread Tools

upload excel and pdf file using Commons FileUpload package

 
 
Guan
Guest
Posts: n/a
 
      11-30-2006
Does anyone know where i can use Jakarta Commons FileUpload package for
uploading excel and pdf file?

using Jakarta Commons FileUpload package with my form
ENCTYPE="multipart/form-data", i am able to upload .txt and .zip.
However, i encountered error when uploading excel and pdf.

Like to find out is it because Jakarta Commons FileUpload package
doesn't support uploading of excel and pdf file or is it because there
might be some problem with my code

Thanks

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      12-09-2006
Guan wrote:
> Does anyone know where i can use Jakarta Commons FileUpload package for
> uploading excel and pdf file?
>
> using Jakarta Commons FileUpload package with my form
> ENCTYPE="multipart/form-data", i am able to upload .txt and .zip.
> However, i encountered error when uploading excel and pdf.
>
> Like to find out is it because Jakarta Commons FileUpload package
> doesn't support uploading of excel and pdf file or is it because there
> might be some problem with my code


FileUpload can upload any file. It basically uploads
a sequence of bytes. It does not care what the content are.

May we see some code ?

Arne
 
Reply With Quote
 
 
 
 
Guan
Guest
Posts: n/a
 
      12-17-2006
Hi Arne,

Add to that i am also using NetComponents class. My purpose is to
create a Web Based ftp to upload the file to the ftp server. So far i
can only upload .txt and .zip. If i upload other file using
ENCTYPE="multipart/form-data". I will receive some error.

Thanks for replying my posting.

================================================== ====================================
/**
*
* This is to upload the file that the client submitted
* to the ftp server.
*
*/


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import com.oroinc.net.ftp.*;

import org.apache.commons.fileupload.servlet.ServletFileU pload;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFac tory;
import org.apache.commons.fileupload.FileUploadException;

public class FtpUpload extends HttpServlet{

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException

{
HttpSession session = request.getSession(true);

if (!session.isNew()){

DataInputStream inputLocalBuf = null;
String fileName=null;

try{

// Check that we have a file upload request
boolean isMultipart =
ServletFileUpload.isMultipartContent(request);

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {

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

if (!item.isFormField()) {

fileName = item.getName();
InputStream uploadedStream = item.getInputStream();
inputLocalBuf = new DataInputStream(uploadedStream);
uploadedStream.close();

}
}
}

catch( FileUploadException fue) {
throw new ServletException("FileUploadError: " +
fue.getMessage(), fue);
}

/* create a UserDetails object. This object will retrieve
* the id & password from the session object*/


UserDetails userDetails = new UserDetails();
userDetails = (UserDetails) session.getAttribute ("userInfo");

String ftpUserId = userDetails.getName();
String ftpPassword = userDetails.getPassword();

String newFileName = null;
StringTokenizer st = new StringTokenizer(fileName,"\\");
while (st.hasMoreTokens()) {
newFileName = st.nextToken();
}


String FTP_SERVER = "192.168.200.100";
String FTP_DIRECTORY = "";

FTPClient ftp = null;

try {
ftp = new FTPClient();
ftp.connect(FTP_SERVER);
int reply = ftp.getReplyCode();
if
(!com.oroinc.net.ftp.FTPReply.isPositiveCompletion (reply))
throw new java.io.IOException("Could not connect to ftp
server " +
FTP_SERVER);
ftp.login(ftpUserId, ftpPassword);
ftp.setFileType(com.oroinc.net.ftp.FTP.BINARY_FILE _TYPE);
ftp.changeWorkingDirectory(ftp.printWorkingDirecto ry());
try {
/* Upload the file to the client ftp /HOME directory*/
//inputLocal = new FileInputStream(objToFtpPut);

if (!ftp.storeFile(newFileName,inputLocalBuf))
throw new java.io.IOException(
"Copy file to remote ftp host failed");
} finally {
if (inputLocalBuf != null) inputLocalBuf.close();
//if (inputLocal != null) inputLocal.close();

/*Display message to inform that file is downloaded
successfully*/
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD>\n"+
"<TITLE>F T P</TITLE></HEAD>\n" +
"<BODY>\n"+
"<CENTER><IMG src=\"/ftp/images/banner.jpg\"
width=\"760\" height=\"75\">\n" +
"</CENTER>\n<BR>\n<BR>\n" +
"<CENTER>\n<TABLE width=\"760\" border=\"0\"
cellspacing=\"0\" cellpadding=\"0\">\n" +
"<TR>\n<TD>\n"+
"<FONT face=\"Verdana, Arial, Helvetica,
sans-serif\"><B><FONT color=\"#000000\">\n" +
"<FONT size=\"2\">&nbsp;M E S S A G
E</FONT></FONT></B></FONT>\n" +
"</TD>\n<TD width=\"615\">\n" +
"<HR size=\"1\" width=\"615\" noshade color=\"#990000\"
align=\"right\">" +
"</TD></TR>\n" +
"<TR>\n<TD>\n"+
"<A href=\"/InvalidateSession\">\n"+
"<FONT face=face=\"Verdana, Arial, Helvetica,
sans-serif\" size=\"1\">[Sign Out]</A>\n"+
"</TD></TR></TABLE>\n"+
"<TABLE width=\"760\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">\n" +
"<TR>\n<TD>\n" +
"Your file have been uploaded <A HREF= \"" +
response.encodeURL("/Validation") +
"\"> Upload another file </A>\n"+
"</TD>\n<TR>\n</TABLE>\n" +
"<TABLE width=\"760\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">" +
"<TR>\n<TD>\n"+
"<HR width=\"760\" align=\"left\" noshade size=\"1\"
color=\"#990000\">"+
"</TD>\n</TR>\n</TABLE>\n</CENTER>\n"+
"</BODY></HTML>");
}
ftp.logout();
} finally {
if (ftp != null) {
try {
ftp.disconnect();
} catch (Exception e) {
}
}
}
}
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using Commons fileupload for progress bar with Javaworldexample lielar Java 1 08-25-2008 11:07 AM
Problems using jakarta commons fileUpload Pablo Java 2 11-21-2006 01:13 AM
Jakarta Commons FileUpload, error in file name extraction. i730lover Java 0 07-21-2006 03:52 PM
Jakarta Commons FileUpload and only *Partially* uploaded files Dundonald Java 2 03-23-2005 02:46 PM
Jakarta Commons FileUpload and only *Partially* uploaded files Dundonald Java 0 03-22-2005 03:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57