Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Updating ProgressBar  RSS feed

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam facing problem with updating progress bar.When i am going to write file(part wise) on outputstream for uploading. Servlet giving error.If I write file whole file at a time then it is working fine.I want to update my progress bar on uploading of file in percentage.Any help will be highly apprecited.
[Error is:java.lang.StringIndexOutOfBounds,length must be >=0]
Below r my codes:-
iam using two class:-
public class ActionDemo6 extends JFrame implements ActionListener
{
public ActionDemo6()
{
setTitle("Upload Interface");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
);
ImageIcon Upload=new ImageIcon("images/Upload.gif");
Button=new JButton(Upload);
Button.setToolTipText("Upload");
Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fileDialog = new FileDialog (ActionDemo6.this );
fileDialog.setMode(FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
{
return;
}
File aa = new File( fileDialog.getDirectory(), fileDialog.getFile()); progressBar.setMaximum(100);
activity=new SimulatedActivity("fileToUploadSt");
activity.start();
activityMonitor.start();
Button.setEnabled(false);
}
});
activityMonitor=new Timer(1000,new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int current=activity.getCurrent();
progressBar.setValue(current);
if(activity.totLengthWritten==activity.getTarget())
{
activityMonitor.stop();
try{
BufferedReader input = new BufferedReader (new InputStreamReader(SimulatedActivity.urlConn.getInputStream ()));
String str;
while ((str = input.readLine())!=null)
{
textArea.append(str);
textArea.append"\n");
}
input.close ();
}
catch(Exception e){textArea.append("Error from input:"+e.toString());}
Button.setEnabled(true);
}
}
});
toolBar.add(Button);
class SimulatedActivity extends Thread
{
private int current;
private int target;
private DataOutputStream printout;
public static URLConnection urlConn;
URL url;
File bb=ActionDemo6.aa;
public InputStream fileIn;
public int totLengthWritten =0;
public SimulatedActivity(String fileToUploadSt)
{

target=(int)bb.length();
current=0;
try
{
fileIn=new FileInputStream(bb);
String str1=ActionDemo6.textArea10.getText();
url = new URL ("http://127.0.0.1:7001/servletUpload?x="+str1);
}
catch(Exception e1){System.out.println("Error from url=" +e1.toString());}
try
{
urlConn = url.openConnection();
}
catch(Exception e1){System.out.println("Error from urlConn=" +e1.toString());}
try
{
urlConn.setDoOutput(true);
}
catch(Exception e1){System.out.println("Error from urlConn=" +e1.toString());}
try
{
urlConn.setUseCaches (false);
}
catch(Exception e1){System.out.println("Error from urlConn=" +e1.toString());}
urlConn.setRequestProperty("Content-Type","application/octet-stream");
try
{
printout = new DataOutputStream(urlConn.getOutputStream ());
}
catch(Exception e1){System.out.println("Error from opening=" +e1.toString());}
}
public void run()
{
int BUFSIZ = 2048;
int k=(int)bb.length();
byte buf[]=new byte[BUFSIZ];
int lengthWritten=0;
do
{
try{ sleep(1000); }
catch(InterruptedException e)
{ return;}
try
{
// upload this buff[] on stream opened on urlconn
System.out.println("value of buf:"+buf);
//My Problem in below lines:-
=============================================================
totLengthWritten+=lengthWritten=fileIn.read(buf,0,BUFSIZ);
System.out.println("value of lengthWritten:"+lengthWritten);
printout.write(buf,0,lengthWritten);
=============================================================

// now the data has being written update current in percentage of file uploaded
try{
current = totLengthWritten * 100 / k;
}
catch(Exception e1){System.out.println("Error from current=" +e1.toString());}
}
catch(Exception e1){System.out.println("Error from write=" +e1.toString());}
}while(totLengthWritten<target);>
}
public int getCurrent()
{
return this.current;
}
public int getTarget()
{
return target;
}
}

Thanks in Advance
Bikash


------------------
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!