I am creating a Web App in which, I have to upload files by splitting them using parallel processing and multi threading and while downloading I have to combine them back to a single file using multi threading and parallel processing.
I want to combine split files into a single. But its not working as I expected to work.
The number of threads created is equal to the number of parts the file have been split.
And the threads should run parallelly and should run only once. But the threads are called several times. Help me fix the code.
UploadServlet.java
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.commons.io.IOUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import jakarta.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class UploadServlet extends HttpServlet
{
private static final long serialVersionUID = 100L;
public static String fileName;
public static long size;
public static int noOfParts;
public static String type;
public static byte[] b;
private static final String INSERT_USERS_SQL = "INSERT INTO uploadlist" +
" (filename, filesize, noofparts) VALUES " +
"(?, ?, ?);";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part file = request.getPart("file");
fileName=file.getSubmittedFileName();
type=file.getContentType();
PrintWriter writer=response.getWriter();
file.write(fileName);
String n = request.getParameter("parts");
size = file.getSize();
Integer temp1 = Integer.parseInt(n);
noOfParts = temp1.intValue();
set();
writer.println("File Uploaded Successfully");
file.delete();
}
public static void set()
{
Split.split(fileName,size,noOfParts);
try {
Connection c = DataBaseConnection.getConnection();
PreparedStatement preparedStatement = c.prepareStatement(INSERT_USERS_SQL);
preparedStatement.setString(1, fileName);
preparedStatement.setLong(2, size);
preparedStatement.setInt(3, noOfParts);
System.out.println(preparedStatement);
preparedStatement.executeUpdate();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
From UploadServlet Split.split() is called to split the files into number of parts.
Split.java
import java.io.*;
import java.util.Arrays;
public class Split implements Runnable
{
int i;
long size;
int noOfParts;
String fileName;
Split()
{
fileName="";
}
Split(String fileName, int i, long size, int noOfParts)
{
this.fileName=fileName;
this.i=i;
this.size=size;
this.noOfParts=noOfParts;
}
public void run()
{
try
{
System.out.println(i);
RandomAccessFile in = new RandomAccessFile("D:\\temp\\"+fileName,"r");
int bytesPerSplit = (int)(size/noOfParts);
int remainingBytes = (int)(size % noOfParts);
byte[] b;
if(i!=noOfParts-1)
{
b = new byte[bytesPerSplit];
}
else
{
b = new byte[bytesPerSplit+remainingBytes];
}
in.seek((long)i*bytesPerSplit);
in.read(b);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Upload\\"+fileName+i+".bin"));
for(byte temp : b)
out.write(temp);
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void split(String fileName, long size, int noOfParts)
{
for(int i=0; i<noOfParts; i++)
{
Split obj = new Split(fileName,i,size,noOfParts);
Thread t = new Thread(obj);
t.start();
}
}
}
In this program, I split the files according to number of parts. And I want to combine them back using Parallel Processing and Multi Threading.
DownloadServlet.java\
import jakarta.servlet.http.HttpServlet;
import org.postgresql.Driver;
import java.sql.Statement;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.util.Arrays;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name = new String(request.getParameter("fileName"));
int noOfParts = Integer.parseInt(request.getParameter("parts"));
int size = Integer.parseInt(request.getParameter("size"));
File downloadFile = new File("D:\\Download\\"+name);
Combine.combine(name,noOfParts,size);
int length = (int)downloadFile.length();
String completeFile=name;
ServletContext context=getServletContext();
String mimeType = context.getMimeType(completeFile);
if (mimeType == null)
{
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength((int)length);
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", completeFile);
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
DataInputStream in = new DataInputStream(new FileInputStream(downloadFile));
byte[] buffer = new byte[(int)length];
while ((in != null) && ((length = in.read(buffer)) != -1))
{
outStream.write(buffer,0,length);
}
if ((length = in.read(buffer))== -1) {
outStream.write(buffer, 0, length);
}
Arrays.fill(buffer, (byte)0);
in.close();
outStream.flush();
outStream.close();
}
}
From DownloadServlet, Combine.combine() is called to combine the split parts into a single file.
Combine.java
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.*;
import java.util.Scanner;
import java.util.Arrays;
public class Combine implements Runnable
{
String name;
int size;
int noOfParts;
int i;
public static String root = "D:\\Upload\\";
Combine(String name,int noOfParts,int size, int i)
{
this.name = name;
this.noOfParts=noOfParts;
this.size=size;
this.i=i;
}
public void run()
{
try
{
System.out.println(i);
RandomAccessFile out = new RandomAccessFile("D:\\Download\\"+name,"rw");
int bytesPerSplit = size/noOfParts;
int remainingBytes = size%noOfParts;
String temp=name+i+".bin";
RandomAccessFile file = new RandomAccessFile(root+temp,"r");
long l=file.length();
byte[] b = new byte[(int)l];
file.read(b);
out.seek(i*bytesPerSplit);
out.write(b);
file.close();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void combine(String name, int noOfParts, int size)
{
for(int i=0; i<noOfParts; i++)
{
Combine obj = new Combine(name,noOfParts,size,i);
Thread t = new Thread(obj,"Thread"+i);
t.start();
}
}
}
I have attached the image in which the numbers represent the part of the file being read and combined using threads.
The output shows that the threads keeping on executing again and again.
I don't know where is the error or any logical mistake in my program.
Help me solve this problem.
Related
Currently working on making a pipeline among two files in java and I would to transmit a float via stream bytes. However I don't know how I can receive it and convert it into a float. Here is what I have done so far:
(3 files)
Consumi.java:
package tryout5_stream_bytes;
import java.io.Serializable;
public class Consumi implements Serializable{
private float consumi = 0.0F;
public Consumi(float consumi){
this.consumi = consumi;
}
public float getConsumi(){
return consumi;
}
public byte[] getBytes(String encode){
return String.valueOf(consumi).getBytes();
}
}
SimulaConsumi.java
package tryout5_stream_bytes;
import java.io.PipedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicBoolean;
public class SimulaConsumi implements Runnable {
private AtomicBoolean isRunning = new AtomicBoolean(false);
private PipedOutputStream pos = null;
public SimulaConsumi(PipedOutputStream pos){
this.pos = pos;
}
#Override
public void run(){
isRunning.set(true);
while(isRunning.get()){
Consumi c = new Consumi((float) (30 * Math.random()));
byte[] message = null;
message = c.getBytes("UTF-8");
try{
pos.write(message);
pos.flush();
} catch(IOException e){
e.printStackTrace();
}
try{
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void terminaSimulaConsumi(){
isRunning.set(false);
}
}
Main.java
package tryout5_stream_bytes;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.io.*;
import java.lang.object;
import java.nio.ByteBuffer;
public class Main {
public static void main(String[] args){
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = null;
try{
pos = new PipedOutputStream(pis);
}catch(IOException e){
e.printStackTrace();
}
SimulaConsumi sc = new SimulaConsumi(pos);
Thread tsc = new Thread();
tsc.start();
while(true){
try{
Thread.sleep(900);
}catch(InterruptedException e){
e.printStackTrace();
}
byte[] buffer = new byte[256];
try{
pis.read(buffer);
}catch(IOException e){
e.printStackTrace();
}
float received = //Get a float from a stream bytes???
System.out.println("Value:"+received);
}
}
}
I believe that the sending of the float in the file "SimulaConsumi" is done well (however I might still be wrong). On the other hand I really have no idea how I can receive it!
In the code below some output is created, some numbers. One of the numbers is a hashvalue. The hashvalue is calculated from a folder.
While the folder is calculated it seems somehow restricted to delete, add and name files. Is this a normal behavior, or could be changed something in the code of the TaskStartPart or TaskPart class ?
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class RestartTest {
StringBuilder sb;
String dtf = "============================";
String hexRes2 = "";
int i1 = 0;
int i2 = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException, IOException, NoSuchAlgorithmException {
// TODO code application logic here
new RestartTest().startApp();
}
public void startApp() throws InterruptedException, IOException, NoSuchAlgorithmException {
TaskStart startTask = new TaskStart();
startTask.startCalc();
}
class TaskStart {
public void startCalc() throws InterruptedException, IOException, NoSuchAlgorithmException {
while(!Thread.currentThread().isInterrupted()) {
i1 = (int) (Math.random() * 1000);
System.out.println("Value 1: " + i1);
new TaskStart2().startCalc2();
new TaskStartPart().calculHash();
dateiSpeichern(i1,i2,"");
}
}
}
class TaskStart2 {
public void startCalc2() throws InterruptedException, IOException {
i2 = (int) (Math.random() * 1000);
System.out.println("Value 2: " + i2);
dateiSpeichern(i1,i2,"");
}
}
class TaskStartPart {
public void calculHash() throws InterruptedException, IOException, NoSuchAlgorithmException {
try {
DigestInputStream digestInputStream=null ;
MessageDigest messageDigest=MessageDigest.getInstance("SHA-512") ;
digestInputStream=new DigestInputStream(new TaskPart(new File("C:\\Users\\win7p\\Documents/t")),messageDigest) ;
//System.out.println("Path :" + direc.toString()) ;
while(digestInputStream.read()>=0) ;
//System.out.print("\nsha-512 sum=") ;
for(byte b: messageDigest.digest()) {
hexRes2 += String.format("%02x",b);
} sb = new StringBuilder(hexRes2);
dateiSpeichern(0,0,sb.substring(hexRes2.length() - 128,hexRes2.length())); System.out.println(sb.substring(hexRes2.length() - 128,hexRes2.length()));
digestInputStream.close();
} catch (IOException ex ) {ex.printStackTrace();}
}
}
class TaskPart extends InputStream {
private File mFile ;
private List<File> mFiles ;
private InputStream mInputStream ;
public TaskPart(File file) throws FileNotFoundException {
mFile=file ;
if(file.isDirectory()) {
mFiles=new ArrayList<File>(Arrays.asList(file.listFiles())) ;
Collections.sort(mFiles) ;
mInputStream=nextInputStream() ;
} else {
mFiles=new ArrayList<File>() ;
mInputStream=new FileInputStream(file) ;
}
}
#Override
public int read() throws IOException {
int result=mInputStream==null?-1:mInputStream.read() ;
if(result<0 && (mInputStream=nextInputStream())!=null)
return read() ;
else return result ;
}
protected String getRelativePath(File file) {
return file.getAbsolutePath().substring(mFile.getAbsolutePath().length()) ;
}
protected InputStream nextInputStream() throws FileNotFoundException {
if(!mFiles.isEmpty()) {
File nextFile=mFiles.remove(0) ;
return new SequenceInputStream(
new ByteArrayInputStream(getRelativePath(nextFile).getBytes()),
new TaskPart(nextFile)) ;
}
else return null ;
}
}
private void dateiSpeichern(int i1, int i2, String hexR) throws InterruptedException, IOException {
try {
String tF = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(new Date().getTime());
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\win7p\\Documents/hashLog.txt", true))) {
writer.append(tF);
writer.newLine();
writer.append(dtf);
writer.newLine();
writer.append("Hash Value: ");
//If(hexR.length() == alHash.get(0))
//alHash.add(hexR);
writer.append(hexR);
writer.newLine();
writer.append("-----");
writer.append("Value 1:");
String si1 = Integer.toString(i1);
writer.append(si1);
writer.newLine();
writer.append("*****");
writer.append("Value 2:");
String si2 = Integer.toString(i2);
writer.append(si2);
writer.newLine();
writer.flush();
writer.close();
}
} catch(IOException ex) {System.out.print("konnte Datei nicht speichern");}
catch(NullPointerException nex) {System.out.println("no Log-File, try again...");}
} }
I think I have find the problem.
In the method protected InputStream nextInputStream() of the class class TaskPart extends InputStream is a List private List mFiles;.
The problem was the List<> remained filled, so it needed to be cleared once the method was called, with mFiles.clear() in calculhash().
So that the files are not longer listed in that stream, and blocked.
Thank you
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm programming a multiple file downloader in Java with JavaFx, but i have some problems with threading.
The problem i have is with the threading part.
I want to start multiple downloads (different urls / files) at the same time, for example two. If i start this two downloadthreads (I think) a race condition happens, because the filename and filesize for both threads are the same and there is also only one file on the HDD, not two as expected.
I am sure it's a race condition problem, but how can i solve it?
Main.java
package de.minimal.program;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import de.minimal.program.model.Download;
import de.minimal.program.util.Dl;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
public class Main extends Application {
private ObservableList<Download> downloadData = FXCollections.observableArrayList();
private int i = 0;
public ObservableList<Download> getDownloadData(){
return downloadData;
}
#Override
public void start(Stage primaryStage) {
downloadData.add(new Download("http://mirror.de.leaseweb.net/videolan/vlc/2.2.1/win32/vlc-2.2.1-win32.exe"));
downloadData.add(new Download("http://releases.ubuntu.com/15.10/ubuntu-15.10-desktop-amd64.iso"));
ArrayList<Thread> t = new ArrayList<Thread>();
ExecutorService executor = Executors.newFixedThreadPool(2, new ThreadFactory() {
#Override
public Thread newThread(Runnable r) {
Thread a = new Thread(r);
a.setName("Thread " + i);
i++;
t.add(a);
return a;
}
});
for(Download dl : downloadData){
Dl d = new Dl(dl);
executor.execute(d);
}
}
public static void main(String[] args) {
launch(args);
}
}
DL.java
package de.minimal.program.util;
import java.util.List;
import de.minimal.program.httpconnection.HttpConnection;
import de.minimal.program.model.Download;
import javafx.concurrent.Task;
public class Dl extends Task<List<Download>> implements Runnable{
private Download download;
private HttpConnection connection;
public Dl(Download download){
this.download = download;
}
#Override
protected synchronized List<Download> call() throws Exception {
connection = new HttpConnection(download);
connection.downloadFile();
return null;
}
}
HTTPConnection.java
package de.minimal.program.httpconnection;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import de.minimal.program.model.Download;
public class HttpConnection {
private static String url;
private Download download;
private static final int BUFFER_SIZE = 4096;
public HttpConnection(Download download){
this.download = download;
}
public void downloadFile() throws IOException{
String saveDir = download.getDownloadSavePath();
url = download.getDownloadUrl();
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
// Forbid redirects for file resuming reasons
connection.setInstanceFollowRedirects(false);
int responseCode = connection.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
String fileName = "";
String disposition = connection.getHeaderField("Content-Disposition");
long contentLength = connection.getContentLengthLong();
boolean appendToFile = false;
if(responseCode == HttpURLConnection.HTTP_PARTIAL)
appendToFile = true;
if(download.getFilesize() == 0){
download.setFilesize(contentLength);
}
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
}
download.setFilename(fileName);
// opens input stream from the HTTP connection
InputStream inputStream = connection.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath, appendToFile);
int bytesRead = -1;
long downloadedBytes = download.getTransferedBytes();
long start = System.currentTimeMillis();
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
downloadedBytes += bytesRead;
if(System.currentTimeMillis() - start >= 2000){
download.setTransferedBytes(downloadedBytes);
start = System.currentTimeMillis();
}
}
outputStream.close();
inputStream.close();
System.out.println("Thread " + Thread.currentThread().getName() + " Filedownload " + fileName + " finished");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
connection.disconnect();
}
}
Download.java
package de.minimal.program.model;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Download {
private final StringProperty filename;
private final StringProperty filepath;
private final LongProperty filesize;
private final LongProperty transferedBytes;
private String downloadUrl;
private String downloadSavePath = "SET PATH ";
public Download(){
this("");
}
public Download(String downloadUrl){
this.downloadUrl = downloadUrl;
this.filename = new SimpleStringProperty(downloadUrl);
this.filepath = new SimpleStringProperty(downloadSavePath);
this.filesize = new SimpleLongProperty(0);
this.transferedBytes = new SimpleLongProperty(0);
}
// Filename
public synchronized String getFilename(){
return filename.get();
}
public synchronized void setFilename(String filename){
System.out.println("Thread " + Thread.currentThread().getName() + " Set filename: " + filename);
this.filename.set(filename);
}
public synchronized StringProperty filenameProperty(){
return filename;
}
// Filepath
public String getFilepath(){
return filepath.get();
}
public void setFilepath(String filepath){
System.out.println("Set filepath: " + filepath);
this.filepath.set(filepath);
}
public StringProperty filepathProperty(){
return filepath;
}
// Filesize
public Long getFilesize(){
return filesize.get();
}
public void setFilesize(Long filesize){
System.out.println("Thread " + Thread.currentThread().getName() + " Set filesize: " + filesize);
this.filesize.set(filesize);
}
public LongProperty filesizeProperty(){
return filesize;
}
// TransferedBytes
public Long getTransferedBytes(){
return transferedBytes.get();
}
public void setTransferedBytes(Long transferedBytes){
System.out.println("Thread " + Thread.currentThread().getName() + " bytes transfered " + transferedBytes);
this.transferedBytes.set(transferedBytes);
}
public LongProperty transferedBytesProperty(){
return transferedBytes;
}
// URL
public String getDownloadUrl(){
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl){
this.downloadUrl = downloadUrl;
}
// SavePath
public String getDownloadSavePath(){
return downloadSavePath;
}
public void setDownloadSavePath(String downloadSavePath){
this.downloadSavePath = downloadSavePath;
}
}
EDIT:
This is the minimal code.
You can add links, start and stop downloads, and change the number of simultaneous concurrent downloads.
EDIT 2:
Minified it again. Hope this time its better.
Adds two downloadlinks and starts them immediately. Reproduces the mentioned problem.
EDIT 3:
Solved it.
The problem was the
private static String url;
I remember that my professor told once that static variables are not thread safe. So more information can found here
10 points about Static in Java point 2
Is writing in a single file a real constraint ? What you could do is to write in separate files, then once the files are complete, merge them into a single one.
Alternatively, if the results from the requests are not so big and could fit in memory, you could directly return the files from the downloading threads, and then write them into a single file.
I Want to separate a text file with separator.
Example
BEGIN:VCARD
VERSION:2.1
X-TIMES_CONTACTED:2
X-LAST_TIME_CONTACTED:1438878121912
N:SomeOne
FN:...
TEL;CELL:111122222
TEL;CELL:333334444
PHOTO;ENCODING=BASE64;JPEG:/...CfbNQMCwwBk1ZtrKadgEXNNC2P/Z
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:Another
FN:Victim
TEL;CELL:888888888888
TEL;CELL:99999999
END:VCARD
with method : SplitFile("c:\test\test.vcf", "END:VCARD", "c:\test\output");
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.FilenameUtils;
public class MainClass {
public static void main(String[] args) {
SplitFile("c:\\test\\test.vcf", "END:VCARD", "c:\\test\\output");
}
private static void SplitFile(String fileName, String seperateChars, String destFolder) {
try {
Path p = Paths.get(fileName);// or
// FileSystems.getDefault().getPath(fileName)
String lines = new String(Files.readAllBytes(p));
String fName = FilenameUtils.getBaseName(p.toString());
String fExt = FilenameUtils.getExtension(p.toString());
int fCounter = 0;
int beginMark = 0;
int endMark = 0;
destFolder = FilenameUtils.normalizeNoEndSeparator(destFolder);
while ((endMark = lines.indexOf(seperateChars, beginMark)) >= 0) {
fCounter++;
String buf = lines.substring(beginMark, endMark + seperateChars.length());
String fOut = String.format("%s\\%s%05d.%s", destFolder, fName, fCounter, fExt);
System.out.println(fOut);
beginMark = endMark + seperateChars.length();
Files.write(Paths.get(fOut), buf.getBytes(), StandardOpenOption.CREATE);
endMark = -1;
}
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
as you have seen before I'm working on a download manager in java, I have asked This Question and I have read This Question But These hadn't solve my problem. now I have wrote another code in java. but there is a problem. when download finishes file is larger than it's size and related software can't read it
This is image of my code execution :
as you see file size is about 9.43 MB
This is My project directory's image:
as you see my downloaded filesize is about 13 MB
So what is my Prooblem?
here is my complete source code
Main Class:
package download.manager;
import java.util.Scanner;
/**
*
* #author Behzad
*/
public class DownloadManager {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter url here : ");
String url = input.nextLine();
DownloadInfo information = new DownloadInfo(url);
}
}
DownloadInfo Class:
package download.manager;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DownloadInfo {
private String downloadUrl;
private String fileName;
private String fileExtension;
private URL nonStringUrl;
private HttpURLConnection connection;
private int fileSize;
private int remainingByte;
private RandomAccessFile outputFile;
public DownloadInfo(String downloadUrl) {
this.downloadUrl = downloadUrl;
initiateInformation();
}
private void initiateInformation(){
fileName = downloadUrl.substring(downloadUrl.lastIndexOf('/') + 1, downloadUrl.length());
fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length());
try {
nonStringUrl = new URL(downloadUrl);
connection = (HttpURLConnection) nonStringUrl.openConnection();
fileSize = ((connection.getContentLength()));
System.out.printf("File Size is : %d \n", fileSize);
System.out.printf("Remain File Size is : %d \n", fileSize % 8);
remainingByte = fileSize % 8;
fileSize /= 8;
outputFile = new RandomAccessFile(fileName, "rw");
} catch (MalformedURLException ex) {
Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.printf("File Name is : %s\n", fileName);
System.out.printf("File Extension is : %s\n", fileExtension);
System.out.printf("Partition Size is : %d MB\n", fileSize);
int first = 0 , last = fileSize - 1;
ExecutorService thread_pool = Executors.newFixedThreadPool(8);
for(int i=0;i<8;i++){
if(i != 7){
thread_pool.submit(new Downloader(nonStringUrl, first, last, (i+1), outputFile));
}
else{
thread_pool.submit(new Downloader(nonStringUrl, first, last + remainingByte, (i+1), outputFile));
}
first = last + 1;
last += fileSize;
}
thread_pool.shutdown();
try {
thread_pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Logger.getLogger(DownloadInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and this is my downloader class:
package download.manager;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* #author Behzad
*/
public class Downloader implements Runnable{
private URL downloadURL;
private int startByte;
private int endByte;
private int threadNum;
private RandomAccessFile outputFile;
private InputStream stream;
public Downloader(URL downloadURL,int startByte, int endByte, int threadNum, RandomAccessFile outputFile) {
this.downloadURL = downloadURL;
this.startByte = startByte;
this.endByte = endByte;
this.threadNum = threadNum;
this.outputFile = outputFile;
}
#Override
public void run() {
download();
}
private void download(){
try {
System.out.printf("Thread %d is working...\n" , threadNum);
HttpURLConnection httpURLConnection = (HttpURLConnection) downloadURL.openConnection();
httpURLConnection.setRequestProperty("Range", "bytes="+startByte+"-"+endByte);
httpURLConnection.connect();
outputFile.seek(startByte);
stream = httpURLConnection.getInputStream();
while(true){
int nextByte = stream.read();
if(nextByte == -1){
break;
}
outputFile.write(endByte);
}
} catch (IOException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This file is MP4 for as you seen, but Gom can't play it
Would you please help me?
OoOoOopppps finally I found what is the problem , It's all on seek method. because i have a file and 8 threads. so seek method changes the cursor repeatedly and make larger file and unexecutable file :), But I'm so sorry . I can't show whole code :)