0

i did file upload in jsp , it uploaded correctly in my program, when i export that into war file and deployed in remote server it not saving my image file into likeimage folder.

i guess my path is not valid in my remote server to save my file..

here my code,

<%@ page import="java.util.List" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.File,java.sql.*" %>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>

        <center><h1>Link Detail Uploaded</h1></center>
   <%!
     String linkhead="";
     String linkval="";
     String filenameval="";

     int count1=0,count2=0,count3=0;
 %>
 <%
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 if (!isMultipart) {
 } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   e.printStackTrace();
   }
   Iterator itr = items.iterator();
   while (itr.hasNext()) 
       {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
       {
          String name = item.getFieldName();
          String value = item.getString();
          if(name.equals("Link_head"))
               {
               linkhead=value;
                     count1=1;
               }
              if(name.equals("Link_val"))
                      {  
                         linkval=value;
                         count2=2;
                      }
              if(name.equals("file"))
              {  
                 filenameval=value;
                 count3=3;
              }




   } else
       {
    try {

   String itemName =item.getName();


   File savedFile = new File(config.getServletContext().getRealPath("/")+"linkimage/"+itemName);
   out.println("\nimage"+itemName);
   out.println("\nimageok: "+filenameval);
   String path=config.getServletContext().getRealPath("/")+"linkimage/"+itemName;
   out.println("\npath     "+path);
   item.write(savedFile);
   Connection con=null;
   Statement st=null;
   try
           {
       Class.forName("com.mysql.jdbc.Driver");
       con=DriverManager.getConnection("jdbc:mysql://localhost:3306/event_db","root","pass@123");
       st=con.createStatement();
       st.executeUpdate("insert into link(link_heading,link_val,image_url) values('"+linkhead+"','"+linkval+"','"+itemName+"')");
   }
catch(Exception e)
        {
    out.println("connetion error"+e);
}
     %><center></table><table ><tr><td width="210"></td><td> <img  border="2" src=linkimage/<%=itemName %> width="137"  height="140"></td></tr></table><table border="2" width="350">

   <% if(count1==1)
             out.println("<tr><td align='left'><b>Link Header:</td><td><b>"+linkhead);
      if(count2==2)  
             out.println("</td><tr><td align='left'><b>Link:</td><td><b>"+linkval);
      } catch (Exception e) {
   e.printStackTrace();
   }
   }
   }
   }
   %>
     </td></tr>

     <tr>
     <td><input type="button"  name="back" value="Back" onClick="javascript:window.location='limkmain.jsp'"></TD>   
        </td>
    <td width="100%" align="center"><a href="linkeditdelete1.jsp" style="font-size:14pt;color:blue;" >Click here to Show Links</a></td>

 </tr>

     </table></center>

give some idea to save my file in remote machine... thanks in advance

1 Answers1

1

You should not write the uploaded file to the webapp's deploy folder. Not only is it disallowed on most hosting environments, but even when it was allowed, all uploaded files would get lost whenever you redeploy the WAR or even when you restart the WAR, simply because those files are not contained in the original WAR.

Instead you should save the uploaded file in a fixed path outside the deploy folder. For example, /var/webapp/upload or something. Contact the serveradmin of the host if that folder can be created with sufficient R/W rights for Java. If that isn't possible, then there's not really another option than saving the whole file in the DB. Either way, downloading the file back can be done by a separate webapp context in case of saving the file in a fixed path on disk (again, it's serveradmin's responsibility to create this), or by a custom servlet in case of saving the file in DB. See also Reliable data serving for details.


Unrelated to the concrete problem, doing this job in a JSP is a bad idea. Use a servlet instead. You've there also some SQL injection holes. Use PreparedStatement instead. Also, the HTML <center> element is deprecated since 1998. Use CSS instead.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555