Uploading and retrieving PDF files using NetBeans5.5
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Benita_Perkins
Posted On:   Wednesday, July 23, 2008 06:22 AM

.js" language="JavaScript1.2">]]>]]> I need to be able to upload and retrieve pdf files. The user will browse and locate the file and click a button to upload the file. This part works - the file is brought to the server and stored. The problem is when the user needs to view the file. Then the application opens the file as html. I have placed mime in the code but the app still opens the files as html. As this is existing code, I haven't changed the way that the code stores the file, which as filename .htm. Could this be causing the problem? The files open fine in older versions of windows but not XP and up. The code for the file upload i   More>>

I need to be able to upload and retrieve pdf files.




The user will browse and locate the file and click a button to upload the file.

This part works - the file is brought to the server and stored.

The problem is when the user needs to view the file. Then the application opens the file as html.

I have placed mime in the code but the app still opens the files as html.


As this is existing code, I haven't changed the way that the code stores the file, which as filename .htm. Could this be causing the problem? The files open fine in older versions of windows but not XP and up.



The code for the file upload is:

			
/* check the number of files
* rename the file
* alter the database row
*/

package cca.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cca.beans.docs_bean; // Class that works with docs
import cca.beans.Settings; // Contains path name

public class docs extends HttpServlet
{
static final private String CONTENT_TYPE = "text/html";


//Initialize global variables
public void init() throws ServletException
{}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
Settings st = new Settings();

// Initialize the class that works with the file
docs_bean d = new docs_bean(); // Class the works with file
String strField = ""; // Stores the field name of the field that we are working with
String strNamingConvention = ""; // Naming convention
String strNumber = ""; // Returned string
int iNumber = 0;
Integer IntTemp = new Integer(0);

// Which field are you going to look at
if (request.getParameter("which").compareTo("Add a scan on file") == 0)
{
strField = "scan";
strNamingConvention = "SC";
}
else if (request.getParameter("which").compareTo("Add a profile on file") == 0)
{
strField = "profile";
strNamingConvention = "PF";
}
else if (request.getParameter("which").compareTo("Add a consumer profile") == 0)
{
strField = "consumer";
strNamingConvention = "CN";
}

try
{
// Get the new number to rename the file
strNumber = d.CheckNumberOfFiles(request.getParameter("regno"), strField); // Get the last number
if (strNumber.startsWith("Application Error")) // If an application error occured
throw new Exception(strNumber);
else
{
Integer Int = new Integer(0);
Int = Int.valueOf(strNumber); // Convert to Integer
iNumber = Int.intValue() + 1; // New number
IntTemp = new Integer(iNumber);
}

// Rename the file
File fl = new File(st.DOCS_PATH + request.getParameter("regno") + "/" + request.getParameter("file1").trim());

File flNew = new File(st.DOCS_PATH + request.getParameter("regno") + "/" + strNamingConvention + IntTemp.toString() + "_" + request.getParameter("regno") + ".htm");
boolean b = fl.renameTo(flNew); // Rename the file
if (b == false) // If the file was not renamed
throw new Exception("File :" + st.DOCS_PATH + request.getParameter("regno") + "/" + request.getParameter("file1").trim() + " could not be renamed.");

// Change the database here
String strReturned = d.Modify_1_Value_DB(strField, iNumber, request.getParameter("regno"));
if (strReturned.startsWith("Application Error")) // If an application error occured
throw new Exception(strReturned);
}
catch(Exception e)
{
session.setAttribute("Error_Variable",e.getMessage());
response.sendRedirect("../ErrorPage.jsp?ErrorLocation=docs");
}

// Redirect to the next page
response.sendRedirect("../docs/add_docs.jsp?cca_key=" + request.getParameter("cca_key") + "&regno=" + request.getParameter("regno") + "&cmpyname=" + request.getParameter("cmpyname") + "&branch=" + request.getParameter("branch") + "&field=" + request.getParameter("field") + "&value=" + request.getParameter("value") + "&which=" + request.getParameter("which") + "&type=Docs");
}
}







The code for the view of the docs is:

			



<% if (request.getParameter("type").compareTo("special") != 0 )
{%>
<%
String strLevel = session.getAttribute("status_level").toString();
if(strLevel.compareTo("-2") != 0 && strLevel.compareTo("1") != 0 && strLevel.compareTo("2") != 0)
{
%> <%
}
}

// Check to see if the folder exists
String str = doc.CheckFolder(request.getParameter("regno"));
boolean bMissing = false;
if (str.compareTo("Missing") == 0)
{
bMissing = true;
}
%>






<% if (request.getParameter("type").compareTo("special") != 0 )
{%>



<%}%>






<% if (bMissing) {%>

<% } else { %>

<%
// check scan on file docs
String strNumber = doc.CheckNumberOfFiles(request.getParameter("regno"), "scan"); // Get the last number
if (strNumber.startsWith("Application Error")) // If an application error occured
{
session.setAttribute("Error_Variable",strNumber);
%> <%
}
else
{
Integer Int = new Integer(0);
Int = Int.valueOf(strNumber); // Convert to Integer
int iNumber = Int.intValue() ; // New number
%> <%
// Write links to file
for(int i = 1; i < iNumber + 1; i++)
{
%> <%
}

}
// check profile on file docs
strNumber = doc.CheckNumberOfFiles(request.getParameter("regno"), "profile"); // Get the last number
if (strNumber.startsWith("Application Error")) // If an application error occured
{
session.setAttribute("Error_Variable",strNumber);
%> <%
}
else
{
Integer Int = new Integer(0);
Int = Int.valueOf(strNumber); // Convert to Integer
int iNumber = Int.intValue() ; // New number
%> <%
// Write links to file
for(int i = 1; i < iNumber + 1; i++)
{
%> <%
}

}
// check consumer profile
strNumber = doc.CheckNumberOfFiles(request.getParameter("regno"), "consumer"); // Get the last number
if (strNumber.startsWith("Application Error")) // If an application error occured
{
session.setAttribute("Error_Variable",strNumber);
%> <%
}
else
{
Integer Int = new Integer(0);
Int = Int.valueOf(strNumber); // Convert to Integer
int iNumber = Int.intValue() ; // New number
%> <%
// Write links to file
for(int i = 1; i < iNumber + 1; i++)
{
%> <%
}

}
}%>

No documents linked to <%=request.getParameter("cmpyname")%> branch <%=request.getParameter("branch")%>
View documents for <%=request.getParameter("cmpyname")%> branch <%=request.getParameter("branch")%>
Scan on file
<%="../uploaded_docs/" + request.getParameter("regno") + "/SC" + new Integer(i).toString() + "_" + request.getParameter("regno") + ".htm"%>" target="_new">Scan on file <%=new Integer(i).toString()%>
Profile on file
<%="../uploaded_docs/" + request.getParameter("regno") + "/PF" + new Integer(i).toString() + "_" + request.getParameter("regno") + ".htm"%>" target="_new">Profile on file <%=new Integer(i).toString()%>
Consumer profile
<%="../uploaded_docs/" + request.getParameter("regno") + "/CN" + new Integer(i).toString() + "_" + request.getParameter("regno") + ".htm"%>" target="_new">Consumer profile <%=new Integer(i).toString()%>












Does anyone have any ideas?



Benita    <<Less
About | Sitemap | Contact