Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How TO Send multipart file from "Java Class" to servlet?

843842Dec 23 2008 — edited Dec 24 2008
Hi All,
I have a big problem:
I have this implemented with JSP to upload files on my servlet.

This code is working correctly:
1)SERVLET
import com.oreilly.servlet.*;*

*//~--- JDK imports ------------------------------------------------------------*

*import java.io.*;

import javax.servlet.*;*
*import javax.servlet.http.*;

public class ControllerFiles extends HttpServlet
    {
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {


        ServletContext context = getServletContext();
        String         forw    = null;

        try
          {

            int              maxUploadSize = 100000000;
            MultipartRequest multi         = new MultipartRequest(request, ".", maxUploadSize);
            String           descrizione   = multi.getParameter("text");
            File             myFile        = multi.getFile("myFile");
            String           filePath      = multi.getOriginalFileName("myFile");
            String           path          = "C:\\files\\";

            try
              {

                FileInputStream inStream = new FileInputStream(myFile);

         
                FileOutputStream outStream = new FileOutputStream(path + myFile.getName());


                while (inStream.available() > 0)
                    {
                    outStream.write(inStream.read());
                    }


                inStream.close();
                outStream.close();
              }
            catch (FileNotFoundException fnfe)
              {
                fnfe.printStackTrace();
              }
            catch (IOException ioe)
              {
                ioe.printStackTrace();
              }

            forw = "/done.jsp";

           
            request.setAttribute("contentType", context.getMimeType(path + myFile.getName()));
            request.setAttribute("text", descrizione);
            request.setAttribute("path", path + myFile.getName());
            request.setAttribute("size", Long.toString(myFile.length()) + " Bytes");

            RequestDispatcher rd = request.getRequestDispatcher(forw);

            rd.forward(request, response);
          }
        catch (Exception e)
          {
            e.printStackTrace();
          }
        }
    }
2)JSP PAGE
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>


    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>

 <form align="center" method="POST" action="ControllerFiles" enctype="multipart/form-data">
    <table align="center" border="2" style="border-collapse: collapse">
        <tr>
            <td align="center"><font size="4" color="yellow">Descrizione</font></td>
            <td colspan="2">
                    <textarea name="text" cols="33" rows="2"></textarea>
            </td>
        </tr>
        <tr>
            <td align="center">File</td>
            <td>
                    <input name="myFile" type="file">
            </td>
            <td align="center">
                    <input type="submit" name="save" value="SAVE">
            </td>
        </tr>
    </table>
</form>


</html>
I need:
Create a Java class to send file to the my servlet without using jsp.
This class will be used in a stand-alone application
Please help me with an example, is very important for me to resolve this problem.

thanks to all

Maxim

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 21 2009
Added on Dec 23 2008
6 comments
580 views