FileOutputStream

FileOutputStream

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:

1)page.jsp:

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

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

2)uploadandstore.jsp

<%@ page import="java.io.*" %>
<%@ 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 = in.read(dataBytes, 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;
saveFile="C:/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully uploaded the file:</b>
<% out.println(f.getName());%></td></tr></table>
<%
}
%>

Thanks









Related Tutorials/Questions & Answers:
FILEOUTPUTSTREAM and FILEINPUTSTREAM - Java Beginners
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 java.io.FileOutputStream. It has two... to a file. The FileOutputStream is the subclass of the OutputStream abstract
Advertisements
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 java.io 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 java.io 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 java.io 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..., construct an OutputStreamWriter on a FileOutputStream. BufferedWriter
How to write a file in Java?
OutputStreamWriter on a FileOutputStream. There are various constructors
Serializable Interface
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 java.io.*; 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 java.io.*; 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..."); if (file.exists()) { FileOutputStream fos = new FileOutputStream(file, false
Stream Cipher
, paramSpec2); FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); CipherOutputStream cos1 = new...); FileOutputStream fos = new FileOutputStream(outputFile); CipherOutputStream cos1
Java Write GZIP File Example
= "myNewCompressedFile.gzip"; FileOutputStream fileOutputStream = new FileOutputStream... */ GZIPOutputStream outputStream = new GZIPOutputStream(fileOutputStream); /* The file
Java File Download Example
to the new file. FileOutputStream fileOutputStream =new FileOutputStream... BufferedOutputStream(fileOutputStream, 1024); Here "sampleFile.txt" is the name... = new BufferedInputStream(url.openStream()); FileOutputStream fileOutputStream
Java Redirect output to file
class by creating an instance of PrintStream class with FileOutputStream...() to the file specified in the FileOutputStream. ADS_TO_REPLACE_1 Here is the code... FileOutputStream("C:/output.dat"))); System.out.println("Hello India
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
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
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
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
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
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
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
files
"); FileOutputStream out = new FileOutputStream("C:/data.txt"); int c; while ((c
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
Property File
("file.properties"); FileOutputStream fos = new FileOutputStream(file
creating new file with File class - JSP-Interview Questions
of FileOutputStream class. If we give the path as from the file uploading location will it work?. For example: FileOutputStream fileOut = new FileOutputStream("C:/Program.../"+num+"."+ext); instead of this if we give the path as FileOutputStream
File Handling - Java Beginners
File Handling  Q. Write a java prg which accepts a list of existing... args[]) { FileInputStream fin1,fin2,fin3; FileOutputStream fout2,fout3... FileOutputStream(args[1]); fout3=new FileOutputStream(args[2
File transfer to teh server - Java Beginners
or FileOutputStream ? this is my code : public class Main { static File... ObjectOutputStream(new FileOutputStream(allinone)); DataOutputStream dos = new DataOutputStream(new FileOutputStream(allinone)); FileInputStream fis=null
Java Write To File From FileInputStream
these streams of bytes you may use FileOutputStream that writes data... of this file in FileOutputStream instance to write the contents that are read from...; FileOutputStream fos = null; int i = 0; try { fis = new FileInputStream("
Reading file into bytearrayoutputstream
and then using FileOutputStream class writes the data into a new file. The fileOutputStream.write(bytes) of the FileOutputStream class is used to write the bytes... to a file FileOutputStream fileOutputStream = new FileOutputStream
XML- read Text Mode - Java Error in Windows - reg. - Java Beginners
FileOutputStream (*) Find & Replace the "Story1" content using regex method (*) Stored... in = new BufferedReader(new InputStreamReader(fis)); FileOutputStream fos = new FileOutputStream(fout); OutputStreamWriter osw = new OutputStreamWriter(fos
Java File Binary
the FileOutputStream and specify the file to create. This stream is then wrapped..., 3.5, 4.5, 5.5 }; File file = new File("C:/student.dat"); FileOutputStream fos = new FileOutputStream(file); DataOutputStream dos = new
Java file DataOutputStream
of FileOutputStream with newfile.txt as the file name to be created. Then we have... { FileOutputStream fos = new FileOutputStream("cities.dat
JSP Open File
'Hello.txt' in the C:drive. The  class FileOutputStream opens an output stream...="java.io.*"%> <% FileOutputStream out;  try{ out = new FileOutputStream("C://Hello.txt"); new PrintStream(out).println
Java program to read a text file and write to another file
. In this example program we will use the FileInputStream and FileOutputStream... and FileOutputStream classes in Java programming language comes with the core Java default... to another file using the FileOutputStream class of Java. ADS_TO_REPLACE_3
SCJP Module-11 Question-9
ts = new Test(); ts.value = 11; try { FileOutputStream fos = new FileOutputStream("savedata.ser"); ObjectOutputStream os = new ObjectOutputStream
POI3.0 - Java Beginners
{ public static void main(String arg[]) { try{ FileOutputStream out = new FileOutputStream("setSheetNameUnicodeDataFormat.xls"); HSSFWorkbook hssfworkbook
java - Java Interview Questions
st = buff.readLine(); File f=new File(st); FileOutputStream file=new FileOutputStream(f); if(f.exists()){ String str="This is simple program
Hiiii - Java Beginners
.... CLOSE THE WHILE LOOP BEFORE FileOutputStream fileOut = new FileOutputStream
Save file on Client Machine - Development process
as FileOutputStream fileOut = new FileOutputStream("\\\\D:\\Public\\DATA
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= new

Ads