• Post Reply Bookmark Topic Watch Topic
  • New Topic

send a file to the server/ multipart request  RSS feed

 
Claudia Vaz
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I�ve a form with a input type file and i want to send that file to the server so that i can read it and treated it.
The code for this is
"<form enctype="multipart/form-data" method="post" action="ver.jsp">
<input type="file" name="ficheiro">
<input type="submit" value="upload">
</form>"
I saw the MultipartRequest class but i don�t know how to use.
Can anyone help me. I am really desperate!!!
Thanks
Claudia
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which MultipartRequest class?

It's not part of the JDK, so you are probably referring to a package like com.oreilly.servlet
www.servlets.com/cos

This package is very easy to use. Just download the cos.jar file, add it to your CLASSPATH (for compiling), make sure it's also somewhere that your servlet runner can pick it up (Tomcat 3.x takes things from CLASSPATH, Tomcat 4.x uses CATALINA_HOME/common/lib), and the code is something like:

 
v velus
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* convert thie programm to JSP */

/*
upload-FileUpload.java
NOTE: This file is a generated file.
Do not modify it by hand!
*/
package upload;

// custom imports for FileUpload
// add your custom import statements here
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.oreilly.servlet.MultipartRequest;
import java.util.*;

public class FileUpload extends javax.servlet.http.HttpServlet
{
protected boolean create() throws java.lang.Exception
{
return true;
}
public FileUpload()
{ // Constructor.
}
private void unhandledEvent( String listenerName, String methodName, java.lang.Object event )
{
}
/**
* destroy Method
*/
public void destroy()
{
super.destroy();
// TODO: implement
}
/**
* doGet Method
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>FileUpload</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>FileUpload</H1>");
out.println("Hello World!");
out.println("<P>Default Implementation From PowerJ</P>");
out.println("</BODY></HTML>");
// TODO: implement
}
/**
* doPost Method
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

PrintWriter out = response.getWriter();
try{
response.setContentType("text/html");
out.println("<HTML>");
out.println("<head><Title>Decoded Uploaded File</title><head>");
out.println("<body>");
out.println("<H1>UploadFile</h1>");
// path must be absolute to upload dir
// This is the decoder class that extracts the parameters and transfer file
// request argument = Http input stream
// c:\\temp\\upload = directory to save file
// 15*1024*1024 = 15mb max size file
MultipartRequest multi = new MultipartRequest(request, "c:\\temp\\upload",15*1024*1024);

// Lists form parameters
out.println("Params:");
Enumeration params = multi.getParameterNames();
out.println("<pre>");
while (params.hasMoreElements()) {
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println(name + " = " + value);
}
out.println("</pre>");

// Show details of uploaded file
out.println("Files:");
Enumeration files = multi.getFileNames();
out.println("<pre>");
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("f.toString(): " + f.toString());
out.println("f.getName(): " + f.getName());
out.println("f.exists(): " + f.exists());
out.println("f.length(): " + f.length());
out.println();
}
out.println("</pre>");
}
}catch (Exception e){
out.println("<pre>");
e.printStackTrace(out);
out.println("</pre>");
}
out.println("</body></html>");
}
/**
* init Method
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
// TODO: implement
}
/****************************************
* data members
****************************************/

// add your data members here
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!