User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 402,900 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,070 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 8055 | Replies: 6
Reply
Join Date: Oct 2006
Posts: 8
Reputation: Mohandsa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mohandsa Mohandsa is offline Offline
Newbie Poster

upload file using jsp and mysql

  #1  
Mar 9th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,240
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: upload file using jsp and mysql

  #2  
Mar 9th, 2007
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
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,701
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: upload file using jsp and mysql

  #3  
Mar 10th, 2007
anyone writing such crap in a JSP gets what he deserves when (and it will) it doesn't work.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Jan 2008
Posts: 1
Reputation: amitshar01 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amitshar01 amitshar01 is offline Offline
Newbie Poster

Re: upload file using jsp and mysql

  #4  
Jan 25th, 2008
Thanks Mohandsa your code was very usefull to me.
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,240
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: upload file using jsp and mysql

  #5  
Jan 25th, 2008
??? usefull, that mess !!!
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,701
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: upload file using jsp and mysql

  #6  
Jan 27th, 2008
Maybe as an example of what not to do?
Or for the IT version of the freaky horror show?
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,378
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 120
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: upload file using jsp and mysql

  #7  
Jan 27th, 2008
Yes, but the Rocky Horror Picture Show is, at least, funny. That's just scary, and, maybe, a little sad. ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 3:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC