I want to stream mp3 files and accessing them using spring, but i dont know how :( have already searched the internet but havent found anything yet. I already tried it using Streams and it worked kinda, but every song starts at the beginning and other people also start at the beginning of the song. My code:
Backend:
new Thread(() -> {
stream = new ByteArrayOutputStream();
while(true){
try {
currentSong = files[rd.nextInt(files.length-1)];
InputStream is = new FileInputStream(new File(currentSong));
int read = 0;
byte[] bytes = new byte[1024];
while((read = is.read(bytes)) !=-1){
stream.write(bytes, 0, read);
}
stream.flush();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();;
Frontend:
public class DnBController {
#GetMapping("/dnb")
public String play(HttpServletResponse httpServletResponse) throws IOException {
OutputStream os = httpServletResponse.getOutputStream();
httpServletResponse.setContentType("audio/mpeg");
DnbradioApplication.stream.writeTo(httpServletResponse.getOutputStream());
return "site.html";
}
I am downloading a zip file from dropbox. When it keeps downloading I measure the file size and get it is increasing its size with the Below code. It downloads whole 84M and after finishing download it turns into 0 bytes. What wrong am I actually doing?
public static void downloadDropBox(File file) {
String url = "https://www.dropbox.com/sh/jx4b2wvqg8d4ze1/AAA0J3LztkRc6FJ5tKy4dUKha?dl=1";
int bytesRead;
byte[] bytesArray = new byte[1024];
InputStream is = null;
FileOutputStream outputStream = null;
long progres = 0;
try {
URL fileUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection)fileUrl.openConnection();
connection.connect();
is = connection.getInputStream();
outputStream = new FileOutputStream(file);
while ((bytesRead = is.read(bytesArray, 0, 1024)) != -1) {
outputStream.write(bytesArray, 0, bytesRead);
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
During download file:
After Finishing download file:
I am making a program which takes a file, and sends it via socket to client. Client receives it and saves it to a file. That is what it is supposed to do.
But somehow, byte array which client receives, contains only 0 bytes, so my output file is empty. here is the code:
Server:
try {
serverSocket=new ServerSocket(7575);
serverSocket.setSoTimeout(1000000);
System.out.println("serverSocket created.");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error in creating new serverSocket on port 7575");
}
for(int i=0;i<array.length;i++)
System.out.println(array[i]);
Socket socket=null;
try {
System.out.println("Waiting for client...");
socket=serverSocket.accept();
System.out.println("Client accepted.");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter outWriter=null;
DataOutputStream outputStream=null;
OutputStream os=null;
BufferedOutputStream bos=null;
try {
os=socket.getOutputStream();
outputStream=new DataOutputStream(os);
outWriter=new PrintWriter(socket.getOutputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
System.out.println("Server streams created.");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("sending name "+name);
outWriter.println(name);
outWriter.flush();
outWriter.println(array.length);
outWriter.println("array.length"+array.length);
outWriter.flush();
try {
os.write(array);
os.flush();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("couldnt send array of bytes");
}
try {
os.close();
outputStream.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
client:
public class Client implements Runnable {
private Socket socket;
private String folderPath;
public Client(String p)
{
folderPath=p;
}
#Override
public void run()
{
try {
System.out.println("Client connecting to localhost on 7575 port...");
socket=new Socket("localhost", 7575);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader reader=null;
BufferedInputStream bis=null;
InputStream input=null;
DataInputStream in=null;
try {
System.out.println("creating streams");
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
input=socket.getInputStream();
in=new DataInputStream(input);
bis=new BufferedInputStream(socket.getInputStream());
System.out.println("streams created!");
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
String name="";
int size=0;
String s="32";
try {
name=reader.readLine();
s=reader.readLine();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if(s!=null)
size=Integer.parseInt(s);
System.out.println("name: "+name);
System.out.println("size: "+size);
byte [] arr=new byte[size];
try {
input.read(arr);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("couldnt read the byte array");
}
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
FileOutputStream fos=null;
try {
fos=new FileOutputStream(folderPath+"/"+name);
} catch (FileNotFoundException ex) {
System.out.println("Could write the file");
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fos.write(arr);
fos.flush();
} catch (IOException ex) {
System.out.println("Could write the file2");
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
try {
in.close();
input.close();
reader.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Mixing binary and text modes on the same stream is tricky. You would be advised not to do it. Using DataInputStream (for the name, count and file content) is one possible solution. (And that is what I would try). Another would be to encode the file content as text (e.g. using Base64 encoding).
The problem with your current "mixed stream: code is on the client side. When you read the name and size from the BufferedReader, you will cause the reader to read and buffer up to 4096 bytes from the socket. The problem is that some of those bytes are file content. So when you then try to read the content from the underlying InputStream here:
input.read(arr);
you may find that there is nothing left to read. Result: an empty or corrupted file.
There's another problem too. Your code assumes that the input.read(arr) statement is going to read the rest of the stream, or until it fills the byte array. This assumption is incorrect. When you are reading from a socket stream, the read is liable to return only the bytes that are currently available (in the client-side network stack).
Once again, the result is liable to be a corrupted file. (In this case truncated.)
The read code should look something like this:
int count = 0;
while (count < size) {
int bytesRead = is.read(bytes, count, bytes.length - count);
if (bytesRead == -1) {
throw EOFException("didn't get a complete file");
}
count += bytesRead;
}
Finally:
Reading the file content into byte arrays at both ends wastes memory, and is going to be problematic for a really large file.
You really should be using "try with resources" to ensure that the streams are all closed properly. Doing it by hand is cumbersome, and risks resource leaks.
you can use DataOutputStream to directly write some string(message) on output stream using writeUTF() function. And then u can receive your message using object of DataInputStream class by using readUTF() method.
u can send data using following:-
String message="something";
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(message);
and u can receive data or message using following:-
DataInputStream in=new DataInputStream(socket.getInputStream());
String message=in.readUTF();
i basically used these method to read data from input stream and write data to outputstream many times and it worked every time, so u should check this way too.
I am making a program which takes a file, and sends it via socket to client. Client receives it and saves it to a file. That is what it is supposed to do.
If you have no need to inspect the content of what is being passed through, then straight InputStream and OutputStream are the way to go, in my opinion. The code is straightforward and fast as it avoids any overhead imposed by higher-level stream types that inspect the content for encoding, etc. This also reduces the opportunity for corrupting the information.
I agree with Stephen C's answer except for
Reading the file content into byte arrays at both ends wastes memory, and is going to be problematic for a really large file.
With the specific requirement to simply move one file to another system with no need to look at the values, this isn't an issue if you know how to handle the content. The basic flow is
client: InputStream in = getFileInputStream();
OutputStream out = socket.getOutputStream();
byte[] bytes = new byte[BUFFER_SIZE]; // could be anything
int bytesRead;
while((bytesRead = in.read(bytes)) != -1){
out.write(bytes,0,bytesRead);
}
in.close();
out.close();
server: InputStream in = socket.getInputStream();
OutputStream out = getFileOutputStream();
// the rest is the exact same thing as the client
This will handle any arbitrarily sized file, limited only by disk size of the server.
Here is an example I whipped up. It's admittedly hacky (the use of the FILE_COUNTER and STOP_KEY for example) but I'm only attempting to show various aspects of having a user enter a file and then send it between a client and server.
public class FileSenderDemo {
private static final int PORT = 7999;
private static final String STOP_KEY = "server.stop";
private static final int[] FILE_COUNTER = {0};
public static void main(String[] args) {
FileSenderDemo sender = new FileSenderDemo();
Thread client = new Thread(sender.getClient());
Thread server = new Thread(sender.getServer());
server.start();
client.start();
try {
server.join();
client.join();
} catch (InterruptedException e) {
FILE_COUNTER[0] = 999 ;
System.setProperty(STOP_KEY,"stop");
throw new IllegalStateException(e);
}
}
public void send(File f, OutputStream out) throws IOException{
try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(f),1<<11)){
byte[] bytes = new byte[1<<11];
int bytesRead;
while((bytesRead = in.read(bytes)) != -1){
out.write(bytes,0,bytesRead);
}
}
}
public Runnable getClient() {
return () -> {
while(FILE_COUNTER[0] < 3 && System.getProperty(STOP_KEY) == null) {
Socket socket;
try {
socket = new Socket("localhost", PORT);
} catch (IOException e) {
throw new IllegalStateException("CLIENT: Can't create the client: " + e.getMessage(), e);
}
File f = getFile();
try (BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream())) {
send(f, out);
} catch (IOException e) {
System.out.println("CLIENT: Failed to send file " + f.getAbsolutePath()+" due to: " + e.getMessage());
e.printStackTrace(System.err);
} finally {
FILE_COUNTER[0]++;
}
}
System.setProperty(STOP_KEY,"stop");
};
}
public File getFile(){
Scanner scanner = new Scanner(System.in);
System.out.println("CLIENT: Enter a file Name: ");
return new File(scanner.next());
}
public Runnable getServer(){
return () -> {
OutputStream out = null;
try{
ServerSocket server = new ServerSocket(PORT);
server.setSoTimeout(20000);
while(System.getProperty(STOP_KEY) == null){
Socket socket = null;
try {
socket = server.accept();
}catch (SocketTimeoutException e){
System.out.println("SERVER: Waited 20 seconds for an accept. Now checking if we need to stop.");
continue;
}
String fileName = "receivedFile_"+System.currentTimeMillis()+".content";
File outFile = new File(fileName);
out = new BufferedOutputStream(new FileOutputStream(outFile));
InputStream in = socket.getInputStream();
int bytesRead;
byte[] bytes = new byte[1<<12];
while((bytesRead = in.read(bytes)) != -1){
out.write(bytes,0,bytesRead);
}
out.close();
socket.close();
System.out.println("SERVER: Just created a new file: " + outFile.getAbsolutePath());
}
System.out.println("SERVER: " + STOP_KEY + " was not null, so quit.");
}catch (IOException e){
throw new IllegalStateException("SERVER: failed to receive the file content",e);
}finally {
if(out != null){
try{out.close();}catch (IOException e){}
}
}
};
}
}
I'll put my code first:
#Post
public Representation post(InputStream zip) throws Throwable {
createFile(zip, "C:/temp");
return new StringRepresentation("File uploaded");
}
public void createFilee(InputStream zipStream, uploadedFileLocation) throws Exception {
try {
writeToFile(zipStream, uploadedFileLocation);
FileUtils.forceDelete(new File(uploadedFileLocation));
} catch (Exception e) {
throw e;
}
}
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
uploadedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
When I send a file to my server, it cannot get deleted. When using FileUtils.forceDelete(), it says that it cannot delete the file. I can delete the file manually while the server is still running after it tries to delete the file with file utils. Why can't it just delete it itself?! any ideas? Thanks
EDIT: Could the issue be that the InputStream from the POST is alive until the POST returns? So, even when I call to delete the file, the stream is still kept alive by the POST? Is this even possible?
In my limited windows experience it can be one of the two things
I would check
1) THe anti-virus software is trying to scan the file
2) Some kind of indexer (System or custom) is trying to index the file.
you can use a tool like processExplorer to see which process holding up the file descriptor.
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
I'm trying to upload file using Axis2 web service by 1024 chunk size.
My server side looks like this:
public void appendChunk(int count, byte[] buffer){
FileOutputStream fos = null;
try {
File destinationFile = new File("c:\\file1.exe");
fos = new FileOutputStream(destinationFile,true);
fos.write(buffer,0, count);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
my client side looks like this:
static int CHUNK_SIZE =1024;
public static void main(String[] args) throws IOException, ServiceException {
FileUploadService strub = new FileUploadServiceLocator();
FileUploadServicePortType a = strub.getFileUploadServiceHttpSoap12Endpoint();
byte[] buffer = new byte[CHUNK_SIZE];
FileInputStream fis = null;
File file = new File("C:\\install.exe");
int count;
try {
fis = new FileInputStream(file);
while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )
{
a.appendChunk(count, buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
fis.close();
}
}
After it the file size is incorrect and if origina file size is 500 Kb, the original size varies between 200 and 400k.
What am I doing wrong?
Update: I looked at log4j file in Tomcat
Nov 17, 2010 2:08:31 PM org.apache.tomcat.util.net.JIoEndpoint createWorkerThread
INFO: Maximum number of threads (200) created for connector with address null and port 80
It looks like all requests to the web server are done Asynchronously and and I also getting IO exception that the file is used by another process.
try to add fos.flush(); before your fos.close(); in your server implementation.
Change
while((count = fis.read(buffer, 0, CHUNK_SIZE)) >0 )
for
while((count = fis.read(buffer, 0, CHUNK_SIZE)) != -1 )