943,520 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 25576
  • JSP RSS
Mar 9th, 2007
0

upload file using jsp and mysql

Expand Post »
i have acode that upload file from client to web server but i need to insert the file name into DB (mysql) to get it in download list............HOW?

this is jsp code
<%@ page contentType="text/html;charset=windows-1252"%>
<%


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("c:\\"+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;

%>
<SCRIPT language="javascript">
history.back(1)
alert('Uploaded <%=lastFileName%>');
window.location.reload(false)
</SCRIPT>
<%
}
out.println("done");
%>
*************************************************
and tis is html code
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"></meta>
<title>untitled1</title>
</head>
<body>
<form name="uploadForm" action="upload.jsp" enctype="multipart/form-data" method="post">
<input type="file" name="file"/>
<input TYPE=Button name='Upload' Value='Upload' onClick="uploadForm.Upload.value='Uploading...';document.uploadForm.action='upload.jsp';document.uploadForm.submit()">
</form>
</body>
</html>

thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mohandsa is offline Offline
11 posts
since Oct 2006
Mar 9th, 2007
0

Re: upload file using jsp and mysql

You don't have to check for type of operation system to use proper slash. Class File provide separator which automaticaly detect OS and apply relevant slash.
For example if your file return absolute path as C:\Test to create new File you do
JSP Syntax (Toggle Plain Text)
  1. File fileTwo = new File(file.getAbsolutePath() + File.separator + fileName);
which will return then absolute path as C:\Test\fileName.In case of unix file return absolute path home/Test return would be home/Test/fileName
Sun tutorial aviable here

I'm not clear how you retrive the files from user pc, but once you get file source destination which you can have as File or String(in case of String you have to create new File out of it) you call method getName()
whitch return String with the name of your file
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Mar 10th, 2007
0

Re: upload file using jsp and mysql

anyone writing such crap in a JSP gets what he deserves when (and it will) it doesn't work.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 25th, 2008
0

Re: upload file using jsp and mysql

Thanks Mohandsa your code was very usefull to me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amitshar01 is offline Offline
1 posts
since Jan 2008
Jan 25th, 2008
0

Re: upload file using jsp and mysql

??? usefull, that mess !!!
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Jan 27th, 2008
0

Re: upload file using jsp and mysql

Maybe as an example of what not to do?
Or for the IT version of the freaky horror show?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 27th, 2008
0

Re: upload file using jsp and mysql

Yes, but the Rocky Horror Picture Show is, at least, funny. That's just scary, and, maybe, a little sad. ;-)
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: jsp and oracle
Next Thread in JSP Forum Timeline: JSP tutorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC