473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

JSP image upload problem in linux server

Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.sql.*,java.io.File" %>
  2. <%@ include file="../../connection/conn.jsp" %>
  3. <%
  4.      String driver = mysql_driver;
  5.     Class.forName(driver).newInstance();
  6.     Connection con=null;
  7.  
  8.     String s_id = (String)session.getAttribute("login_ses_id");
  9.      //response.setContentType("text/html");
  10.     //response.setHeader("Cache-control","no-cache");
  11.  
  12.     //String err = "";
  13.  
  14.     //String lastFileName = "";
  15.  
  16.     //String contentType = request.getContentType();
  17.     //String boundary = "";
  18.     String msg="";
  19.     String institute_id = null;
  20.     ResultSet rst=null;
  21.     Statement stmt=null;
  22.  
  23.     try
  24.     {
  25.     String url=mysql_con_string;
  26.     con=DriverManager.getConnection(url);
  27.     stmt=con.createStatement();
  28.     //stmt1=con.createStatement();
  29.  
  30.     rst=stmt.executeQuery("select * from institutes join users on users.user_institute_id = institutes.institute_id join subscription on subscription.s_institute_id = institutes.institute_id where user_id='"+s_id+"'");
  31.  
  32.         while(rst.next()){
  33.         institute_id  = rst.getString("institute_id");
  34.         }
  35.  
  36.         }catch(Exception e){
  37.     out.println(e.getMessage());
  38. }
  39.  
  40.  
  41.      response.setContentType("text/html");
  42.     response.setHeader("Cache-control","no-cache");
  43.  
  44.     String err = "";
  45.  
  46.     String lastFileName = "";
  47.  
  48.     String contentType = request.getContentType();
  49.     String boundary = "";
  50.     final int BOUNDARY_WORD_SIZE = "boundary=".length();
  51.     if(contentType == null || !contentType.startsWith("multipart/form-data")) {
  52.       err = "Ilegal ENCTYPE : must be multipart/form-data\n";
  53.       err += "ENCTYPE set = " + contentType;
  54.     }else{
  55.       boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
  56.       boundary = "--" + boundary;
  57.       try {
  58.         javax.servlet.ServletInputStream sis = request.getInputStream();
  59.         byte[] b = new byte[1024];
  60.         int x=0;
  61.         int state=0;
  62.         String name=null,fileName=null,contentType2=null;
  63.         java.io.FileOutputStream buffer = null;
  64.         while((x=sis.readLine(b,0,1024))>-1) {
  65.           String s = new String(b,0,x);
  66.           if(s.startsWith(boundary)) {
  67.             state = 0;
  68.             //out.println("name="+name+"<br>");
  69.             //out.println(fileName+"<br>");
  70.  
  71.             name = null;
  72.             contentType2 = null;
  73.             fileName = null;
  74.  
  75.  
  76.           }else if(s.startsWith("Content-Disposition") && state==0) {
  77.             state = 1;
  78.             if(s.indexOf("filename=") == -1)
  79.               name = s.substring(s.indexOf("name=") + "name=".length(),s.length()-2);
  80.             else {
  81.               name = s.substring(s.indexOf("name=") + "name=".length(),s.lastIndexOf(";"));
  82.               fileName = s.substring(s.indexOf("filename=") + "filename=".length(),s.length()-2);
  83.               if(fileName.equals("\"\"")) {
  84.                 fileName = null;
  85.               }else {
  86.                 String userAgent = request.getHeader("User-Agent");
  87.                 String userSeparator="/";  // default
  88.                 if (userAgent.indexOf("Windows")!=-1)
  89.                   userSeparator="\\";
  90.                 if (userAgent.indexOf("Linux")!=-1)
  91.                   userSeparator="/";
  92.                 fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
  93.                 if(fileName.startsWith( "\""))
  94.                   fileName = "";
  95.               }
  96.             }
  97.             name = name.substring(1,name.length()-1);
  98.             if (name.equals("file")) {
  99.               if (buffer!=null)
  100.                 buffer.close();
  101.               lastFileName = fileName;
  102.               //getServletContext().getRealPath("/images/"+institute_id+".jpg");
  103.  
  104.               buffer = new java.io.FileOutputStream("/opt/tomcat6/webapps/study.lk/admin/institutions/images/"+institute_id+".jpg");
  105.  
  106.                   //String f = getServletConfig().getServletContext().getRealPath();
  107.                   //out.println("");
  108.  
  109.             }
  110.           }else if(s.startsWith("Content-Type") && state==1) {
  111.             state = 2;
  112.             contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
  113.           }else if(s.equals("\r\n") && state != 3) {
  114.             state = 3;
  115.           }else {
  116.             if (name.equals("file"))
  117.               buffer.write(b,0,x);
  118.           }
  119.         }
  120.         sis.close();
  121.         buffer.close();
  122.       }catch(java.io.IOException e) {
  123.         err = e.toString();
  124.       }
  125.     }
  126.     boolean ok = err.equals("");
  127.     if(!ok) {
  128.      out.println();
  129.     }else{
  130.     msg = "Successfully Updated!!!";
  131.  
  132.     }
  133. %>
  134.  
  135. <script language="javascript">
  136.  
  137. function valLogo(){
  138.  
  139. //user_group = document.registration_form.groups.selectedIndex;
  140. file = document.uploadForm.file.value;
  141.  
  142.     if (file.length==0)
  143.     {
  144.  
  145.         document.getElementById('errorlogo').innerHTML = 'Please first select the image from file';
  146.         document.uploadForm.file.focus();
  147.  
  148.         return false;
  149.     }
  150. }
  151. </script>
  152. <link href="../../stylesheets/text.css" rel="stylesheet" type="text/css" />
  153. <form name="uploadForm" onsubmit="return valLogo()" action="upload.jsp" enctype="multipart/form-data" method="post">
  154.   <table width="100%" border="0" cellspacing="0" cellpadding="0">
  155.     <tr>
  156.       <td colspan="3"><div align="center" class="headline">Add / Edit Logo</div></td>
  157.     </tr>
  158.     <tr>
  159.       <td>&nbsp;</td>
  160.       <td>&nbsp;</td>
  161.       <td>&nbsp;</td>
  162.     </tr>
  163.     <tr>
  164.       <td>&nbsp;</td>
  165.       <td colspan="2" id="errorlogo" class="error"><%=msg%></td>
  166.     </tr>
  167.     <tr>
  168.       <td width="5%">&nbsp;</td>
  169.       <td width="28%">&nbsp;</td>
  170.       <td width="67%">&nbsp;</td>
  171.     </tr>
  172.     <tr>
  173.       <td>&nbsp;</td>
  174.       <td><input name="file" type="file" class="text"/></td>
  175.       <td><input name='Upload'  type="submit" class="text" value='Upload'/></td>
  176.     </tr>
  177.     <tr>
  178.       <td height="57" colspan="3"><p>&nbsp;</p>
  179.       <p align="center"><a href="javascript:window.close();" class="links"><strong>( Close This Window )</strong></a></p></td>
  180.     </tr>
  181.   </table>
  182. </form>
This coding properly working on windows. But when i upload it to linux server images not saving in that folder. But i gave correct path for saving images in linux server. Can anybody help me on this????
Dec 1 '08 #1
3 4190
Nepomuk
3,112 Expert 2GB
If the directory /images exists in the root file system (and all other directories are correct too), then the only thing I could find is line 113:
Expand|Select|Wrap|Line Numbers
  1. }else if(s.equals("\r\n") && state != 3) {
"\r\n" is used within textfiles in Windows, but not in Linux (normally). Linux (normally) just uses "\n" instead. I think MACs do it slightly different from both. I'd suggest, if you want to check for Windows or Linux line breaks, use
Expand|Select|Wrap|Line Numbers
  1. }else if((s.equals("\r\n") || s.equals("\n")) && state != 3) {
I don't know, if this will solve your problem, but it certainly might help.

Greetings,
Nepomuk
Dec 1 '08 #2
pronerd
392 Expert 256MB
@dirtyhawk
Not with out any details. What is happening when you try? Either that code is never reached, or an exception is thrown. If it is an exception what does it say?

If you have access to the Linux server try the command below and see if you get an error.

Expand|Select|Wrap|Line Numbers
  1. ls /opt/tomcat6/webapps/study.lk/admin/institutions/images/
P.S. Instead of encoding system specific paths in your code it would be better to use the servlet API to get the path of your server, and then just add the relative path to your app inside the server. Then use the File API to avoid system dependent separators. See below.

Expand|Select|Wrap|Line Numbers
  1.  
  2. String serverPath = getServletConfig().getServletContext().getRealPath();
  3.  
  4. String imagePath = serverPath + "study.lk"+ File.separator +"admin"+ File.separator +"institutions"+ File.separator +"images"+ File.separator;
  5.  
  6.  
Dec 2 '08 #3
It's working now. Path is ok. problem was folder permission. i have to enable write permission.

Thanks everybody.
Dec 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: lawrence | last post by:
I've the form you see below, which is supposed to upload an image. I expect the image name as a var to be $uploadedFile. In the receiving function, I've this code: global $uploadedFile,...
1
by: geradeaus | last post by:
Even if you upload an image partially with ftp software, you can still read the image. Only the image will be partially gray. So, can I determine with a php-function if an image is fully uploaded...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
0
by: Paul Hamlington | last post by:
Hello, I've been programming in ASP for a little while now and quite an advanced user, but I have come across an unusual problem in which I need assistance. I have built my own image upload,...
2
by: Gary \(Girish\) | last post by:
Hello: I am using SA-FileUp to enable members to upload their product images to my ISP sever. When member uploaded a new image file, the file name is replaced with the member ID and the old file...
1
by: John Thompson | last post by:
We're sooo close. When we load the page to upload the image, all of the prms go through except the binary image data. Using SQL server with the data type set to "image". Please help! Thanks-...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
2
kamill
by: kamill | last post by:
Dear All, I am uploading a image file and after resize the image i am saving it. My script is working fine with small size images but when i am trying to upload big size image, i am getting below...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.