Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to change content type from multipart/form-data to text/html

 
Karuna chauhan
Greenhorn
Posts: 13
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! all,

I have four components in my html form(enctype="multipart/form-data").First two components are text fields of "text" type and third one is of "file" type(for file uploading)and a "submission Button".When i am recieving the data of html form on a jsp page using the "request" object, it is giving NULL values for first two fields.I store the recieved data into table "photogallery(photoid,description,photo)" using the database-SQL Server 2000. What could be the probable solution for this? Below is the coding that for jsp file:

uploading.jsp:
<------------------------------------------------------------------------->
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>
<%@ page import="java.io.FileWriter,java.io.IOException" %>
<%@page import="java.sql.*"%>
<%@page errorPage="error.jsp"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
request.setContentType("text/html");
int photoid=Integer.parseInt(request.getParameter("txtid"));
String desc=request.getParameter("txtdesc");
out.println(photoid+desc);
%>
<%
response.setContentType("text/html");
response.setHeader("Cache-control","no-cache");

String err = "";

String lastFileName = "";

String contentType = request.getContentType();
String boundary = "";
final int BOUNDARY_WORD_SIZE = "boundary=".length();
if(contentType == null || !contentType.startsWith("multipart/form-data")) {
err = "Ilegal ENCTYPE : must be multipart/form-data\n";
err += "ENCTYPE set = " + contentType;
}else{
boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
boundary = "--" + boundary;
try {
javax.servlet.ServletInputStream sis = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
int state=0;
String name=null,fileName=null,contentType2=null;
java.io.FileOutputStream buffer = null;
while((x=sis.readLine(b,0,1024))>-1) {
String s = new String(b,0,x);
if(s.startsWith(boundary)) {
state = 0;
//out.println("name="+name+"<br>");
//out.println(fileName+"<br>");

name = null;
contentType2 = null;
fileName = null;


}else if(s.startsWith("Content-Disposition") && state==0) {
state = 1;
if(s.indexOf("filename=") == -1)
name = s.substring(s.indexOf("name=") + "name=".length(),s.length()-2);
else {
name = s.substring(s.indexOf("name=") + "name=".length(),s.lastIndexOf(";"));
fileName = s.substring(s.indexOf("filename=") + "filename=".length(),s.length()-2);
if(fileName.equals("\"\"")) {
fileName = null;
}else {
String userAgent = request.getHeader("User-Agent");
String userSeparator="/"; // default
if (userAgent.indexOf("Windows")!=-1)
userSeparator="\\";
if (userAgent.indexOf("Linux")!=-1)
userSeparator="/";
fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
if(fileName.startsWith( "\""))
fileName = fileName.substring( 1);
}
}
name = name.substring(1,name.length()-1);
if (name.equals("file")) {
if (buffer!=null)
buffer.close();
lastFileName = fileName;
buffer = new java.io.FileOutputStream("/TestAlumni/web/Upload/"+fileName);
}
}else if(s.startsWith("Content-Type") && state==1) {
state = 2;
contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
}else if(s.equals("\r\n") && state != 3) {
state = 3;
}else {
if (name.equals("file"))
buffer.write(b,0,x);
}
}
sis.close();
buffer.close();
}catch(java.io.IOException e) {
err = e.toString();
}
}
boolean ok = err.equals("");
if(!ok) {
out.println(err);
}else{
String fn=lastFileName;

PreparedStatement ps = con.prepareStatement ("Insert into photogallery values(?,?,?)");
ps.setInt(1,photoid);
ps.setString(2,desc);
ps.setString(3,fn);
ps.executeUpdate();
ps.close();
con.close();

//response.sendRedirect(".jsp");
%>
<SCRIPT language="javascript">
history.back(1)
alert('Uploaded <%=lastFileName%>');
location.href="adminPhoto.jsp"
</SCRIPT>
<%
}
out.println("done");
%>

</body>
</html>

<--------------------------------------------------------------------->
Thanks in advance
[ October 26, 2008: Message edited by: Karuna chauhan ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request object does not know how to parse multi-part requests, which is why you are getting null values.

Parsing a multi-part request is a complicated affair and, rather than trying to do it yourself, you'd be better served by using one of the libraries that have already solved the problem for you. See the JSP FAQ on file uploading for more details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic