Link to home
Start Free TrialLog in
Avatar of ahwind
ahwind

asked on

Upload sound file and store path in database

Dear all,

My website is about learning German language. the administrator can add new exercise question such as dictation. so the admin should specify the path (input type = file) of the sound file.how should i write the jsp code to upload the sound file to web server and store the name of sound file?thanks in avance

Avatar of CodingExperts
CodingExperts

Hi ahwind,
You can see an example on the link
http://www.oop-reserch.com/mime_example_3.html

This example explains:

How to set the maximum size of the file to be uploaded ?
How to restrict the mime-type (Content-Type) of the file to be uploaded. For example, if you'd like to allow the users to upload the files only of audio type? how can you do it?
How to check the binary contents of the uploaded file?

I hope this should suffice ur needs .if in case of any doubt do write back..

Good Luck
Coding Experts
Hi ahwind,
May be this sample code may help you solve your problem.

<!-- upload.jsp -->
<%@ page import="java.io.*" %>

<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}

String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

//out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;


int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

out.println("File saved as " +saveFile);

}
%>
ASKER CERTIFIED SOLUTION
Avatar of CodingExperts
CodingExperts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ahwind

ASKER

Dear CodingExperts,

I have solved my problem regarding the upload file into web server. I used the MultipartRequest class written by Jason Hunter under servlets.com.  Go here to download the jar: http://www.servlets.com/cos/cos-05Nov2002.zip

Put cos.jar in your classpath.  Then write a servlet that accepts the request as a MultipartRequest object and you will be able to pull out the uploaded files... There is lots of documentation on how to use these classes on the internet including here:  http://www.servlets.com/cos/index.html


1) Download the zip and put "cos.jar" on classpath

2) Create an HTML form with upload button in a file called "form.html":

<form action="test.jsp" method="post" enctype="multipart/form-data">
<input type=file name=testfile>
<input type=submit>
</form>

3) Create an empty directory in "c:/upload"

4) Make a file called "test.jsp", and put:

<%@ page import="com.oreilly.servlet.*" %>
<%
 MultipartRequest mp = new MultipartRequest(request, "c:/upload");
 out.println("DONE");
%>

5) The file should have been uploaded.  You can find more command on what you cando with the MultipartRequest here: http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html