File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes how to change content type from multipart/form-data to text/html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to change content type from multipart/form-data to text/html" Watch "how to change content type from multipart/form-data to text/html" New topic

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

Karuna chauhan

Joined: Oct 11, 2007
Posts: 13

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:

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

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
int photoid=Integer.parseInt(request.getParameter("txtid"));
String desc=request.getParameter("txtdesc");

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;
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; buffer = null;
while((x=sis.readLine(b,0,1024))>-1) {
String s = new String(b,0,x);
if(s.startsWith(boundary)) {
state = 0;

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)
if (userAgent.indexOf("Linux")!=-1)
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)
lastFileName = fileName;
buffer = new"/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"))
}catch( e) {
err = e.toString();
boolean ok = err.equals("");
if(!ok) {
String fn=lastFileName;

PreparedStatement ps = con.prepareStatement ("Insert into photogallery values(?,?,?)");

<SCRIPT language="javascript">
alert('Uploaded <%=lastFileName%>');


Thanks in advance
[ October 26, 2008: Message edited by: Karuna chauhan ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60049

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: how to change content type from multipart/form-data to text/html
Similar Threads
error in dwonloading a pdf file
Array Index Out Of Bounds
File Upload using JSP
Problem uploading file togetherwith database insertion
changing filename after uploading image