Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa JSP-Servlet FileOutputStream

View Questions and Answers by Category


Have Programming Question? Ask it here!

1 Answer(s)      4 years and 6 months ago
Posted in : JSP-Servlet

When uploading a file in JSP through FileOutputStream object it requires the full file path to be given. Otherwise it shows an error as writing the file with the already existing one and writing the ontent in the same file. But, while giving the path from drive it does not show any error.

How to solve this problem. Please help me.

View Answers

May 20, 2010 at 11:28 AM

Hi Friend,

Try the following code:


<%@ page language="java" %>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"uploadandstore.jsp" METHOD=POST>
<table border="0" bgcolor=#ccFDDEE>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
<td colspan="2" align="center">&nbsp;</td>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
<td colspan="2" align="center">&nbsp;</td>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>


<%@ page import="*" %>
<%@ page import="java.sql.*" %>
String saveFile="";
String contentType = request.getContentType();
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 =, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
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;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%><Br><table border="2"><tr><td><b>You have successfully uploaded the file:</b>
<% out.println(f.getName());%></td></tr></table>


Related Tutorials/Questions & Answers:
FILEOUTPUTSTREAM and FILEINPUTSTREAM  Using FILEOUTPUTSTREAM and FILEINPUTSTREAM provide code that will write bytes to file and read them back.   Hi Friend, Please visit the following links: http
fileoutputstream example
fileoutputstream example We are going to discuss about fileoutputstream in java. The fileoutputstream class extends the It has two... to a file. The FileOutputStream is the subclass of the OutputStream abstract
FileOutputStream - JSP-Servlet
FileOutputStream  When uploading a file in JSP through FileOutputStream object it requires the full file path to be given. Otherwise it shows...); FileOutputStream fileOut = new FileOutputStream(f); fileOut.write(dataBytes
How to write to file using FileOutputStream
help me in java program. How to write to file using FileOutputStream? thanks,   Hi, To write a file using FileOutputStream, we have to use FileOutputStream class of package. By using this FileOutputStream class
FileOutputStream example code
Writing a file using FileOutputStream... FileOutputStream As we discussed earlier, java has two kinds of streams- Byte & Characters.... The OutputStream abstract class is used to write data to a file. The FileOutputStream
Java FileOutputStream Example
Java FileOutputStream Example In this section we will discuss about the Java IO FileOutputStream. FileOutputStream is a class of package which facilitate to write output stream data to a file. The FileOutputStream writes
File location in FileOutputStream - JSP-Servlet
File location in FileOutputStream  Hai, For uploading a file i used the FileOutputStream() method. And uploading works perfectly. This method allows parameter as file name or the full path(that is from the drive letter
Java Write To File FileOutputStream
Java Write To File FileOutputStream In this tutorial you will learn how to write to file using FileOutputStream. Write to file using FileOutputStream you will have to use the FileOutputStream class of package. This class
SCJP Module-11 Question-8
; try { 7    FileOutputStream fs = new FileOutputStream("
SCJP Module-11 Question-7
dataOutputStream(new FileOutputStream("student.ser")); Choose the correct
SCJP Module-11 Question-1
(String[] args) { Test f = new Test(); try { FileOutputStream fs = new FileOutputStream("serializable.ser"); ObjectOutputStream os = new
SCJP Module-11 Question-3
[] args) { Test b = new Test(); try { FileOutputStream fs = new FileOutputStream("file.ser"); ObjectOutputStream os = new ObjectOutputStream(fs
How to Write to a File in Java
will be able to use OutputStreamWriter on a FileOutputStream classes to write data... an OutputStreamWriter on a FileOutputStream. BufferedWriter The BufferWriter
How to write a file in Java?
OutputStreamWriter on a FileOutputStream. There are various constructors of FileWriter class
Difference between Java IO Class - Java Beginners
& FileOutPutStream vs. 2)InputStreamReader & OutputStreamWriter   Hifriend, 1. The FileOutputStream class is a subclass of OutputStream. 2. You can construct a FileOutputStream object by passing
jsp to excel - JSP-Servlet
jsp to excel  Hi All, I'm writing a program of jsp to Excel conversion with Using POI. My problem is i'm unable to put multiple data into excel...)); } FileOutputStream fileOut = new FileOutputStream("c:\\excel\\wct.xls
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import*; class..."); FileOutputStream out = new FileOutputStream("C:/data.txt"); int c
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import*; class..."); FileOutputStream out = new FileOutputStream("C:/data.txt"); int c
Java file overwrite
. As the FileOutputStream class consists of two parameters: FileOutputStream (File file, boolean append) So the constructor can be written as FileOutputStream ("myFile.txt...()) { FileOutputStream fos = new FileOutputStream(file, false); fos.write(st.getBytes
Java Redirect output to file
class by creating an instance of PrintStream class with FileOutputStream...() to the file specified in the FileOutputStream. Here is the code: import throws Exception { System.setOut(new PrintStream(new FileOutputStream("C
Java Write GZIP File Example
= "myNewCompressedFile.gzip"; FileOutputStream fileOutputStream = new FileOutputStream... */ GZIPOutputStream outputStream = new GZIPOutputStream(fileOutputStream); /* The file
Stream Cipher
, paramSpec2); FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); CipherOutputStream cos1 = new... FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile
Java File Download Example
to the new file. FileOutputStream fileOutputStream =new FileOutputStream... BufferedOutputStream(fileOutputStream, 1024); Here "sampleFile.txt" is the name...(url.openStream()); FileOutputStream fileOutputStream =new FileOutputStream
Java file DataOutputStream
of FileOutputStream with newfile.txt as the file name to be created. Then we have... { FileOutputStream fos = new FileOutputStream("cities.dat"); DataOutputStream dos = new
Adding images in itext pdf
Adding images in itext pdf  Hi, How to add image in pdf file using itext? Thanks   Hi, You can use following code: PdfWriter.getInstance(document,new FileOutputStream("imagesPDF.pdf")); Read more at Inserting
JAVA Objectives Question?
for the book's title and then write it to the file by using a FileOutputStream. Save... have used FileInputStream and FileOutputStream for I/O operations. import...(); FileOutputStream fos=new FileOutputStream(f); byte buf[] = str.getBytes
Property File
(""); FileOutputStream fos = new FileOutputStream(file
how to present the excel to the web browser in jsp
"); XSSFSheet sheet4 = wb.createSheet("OTHERS"); FileOutputStream fileOut = new FileOutputStream ("c:\\excel\\test.xlsx"); wb.write(fileOut
"); FileOutputStream out = new FileOutputStream("C:/data.txt"); int c; while ((c
File not found
File not found  byte[] buffer = new byte[totalBytesRead]; try{ FileOutputStream fos = new FileOutputStream(destFolder+emp_code+".zip"); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze