开场DataInputStream以运行速度非常慢 [英] Opening DataInputStream running very slow
问题描述
我文件上载到服务器,并根据处理的文件,我从服务器获取不同的答复。一切工作,然而获得来自服务器的应答很慢。我在调试器中检查和code以下行正在采取6秒运行。
= inStream中DataInputStream所新(connection.getInputStream());
我已经测试过相同的文件和code在Web浏览器,它完美的,需要大约1或2秒显示答复。这里是我的全code,我想它的确定,但也许有东西在这里是没有做好。是否有这样做的更好的办法?或者是新的DataInputStream以总是会这么慢?
私人字符串loadImageFromNetwork(字符串MYFILE){
HttpURLConnection的连接= NULL;
DataOutputStream类outStream = NULL;
DataInputStream以inStream中= NULL;
字符串化妆=;
弦模型=;
串DISP =;
字符串lineEnd =\\ r \\ n;
串twoHyphens = - ;
字符串边界=*****; INT读取动作,方bytesAvailable,缓冲区大小; 字节[]缓冲区; INT MAXBUFFERSIZE = 1 * 1024 * 1024; 字符串urlString =HTTP://xxxxxxxxxxxxxx/upload.php
sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ MYFILE))); 尝试{ 的FileInputStream的FileInputStream =新的FileInputStream(新文件(MYFILE)); //打开一个URL连接到这个Servlet
网址URL =新的URL(urlString); //打开HTTP连接的URL
连接=(HttpURLConnection类)url.openConnection(); //允许输入
connection.setDoInput(真); //允许输出
connection.setDoOutput(真); //不要使用缓存副本。
connection.setUseCaches(假); //使用POST方法。
connection.setRequestMethod(POST); connection.setRequestProperty(连接,保持活动); connection.setRequestProperty(内容类型,的multipart / form-data的;边界=+边界); outStream =新的DataOutputStream类(connection.getOutputStream()); outStream.writeBytes(twoHyphens +边界+ lineEnd);
outStream.writeBytes(内容处置:表格数据;名称= \\UploadedFile的\\;文件名= \\+ MYFILE +\\+ lineEnd);
outStream.writeBytes(lineEnd); //创建最大大小的缓冲区
参考bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
缓冲区=新的字节[缓冲区大小] //读取文件,并将其写入形式... 读取动作= fileInputStream.read(缓冲液,0,缓冲区大小); 而(读取动作大于0){
outStream.write(缓冲液,0,缓冲区大小);
参考bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
} //发送文件数据后necesssary多部分的表单数据...
outStream.writeBytes(lineEnd);
outStream.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd); //关闭流
fileInputStream.close();
outStream.flush();
outStream.close();
}
赶上(MalformedURLException的前){
ex.printStackTrace();
} 赶上(IOException异常IOE){
ioe.printStackTrace();
} // ------------------读取服务器响应
尝试{ inStream中=新DataInputStream所(connection.getInputStream()); 字符串str; 而((海峡= inStream.readLine())!= NULL)
{
DISP = DISP + STR; } inStream.close();
}
赶上(IOException异常ioex){
ioex.printStackTrace();
}
返回DISP; }
您应该移动code到从服务器读取到一个新的线程的响应。例如:
私有类ReadResponse实现Runnable {
公共无效的run(){
// ------------------读取服务器响应
尝试{ inStream中=新DataInputStream所(connection.getInputStream()); 字符串str; 而((海峡= inStream.readLine())!= NULL)
{
DISP = DISP + STR; } inStream.close();
}
赶上(IOException异常ioex){
ioex.printStackTrace();
}
//返回DISP;
//这里你需要证明你的UI线程显示
}
}
}
和上传文件之前开始读取线程。
I am uploading a file to a server and depending on the processing on the file I get a different reply from the server. Everything is working, however getting the reply from the server is very slow. I checked in the debugger and the following line of code is taking 6 seconds to run.
inStream = new DataInputStream( connection.getInputStream() );
I have tested the same files and code over a web browser and its perfect, taking about 1 or 2 seconds to display the reply. Here is my full code, I think its ok, but maybe there is something here that is not done properly. Is there a better way of doing this? Or is a new DataInputStream always going to be so slow?
private String loadImageFromNetwork(String myfile) {
HttpURLConnection connection = null;
DataOutputStream outStream = null;
DataInputStream inStream = null;
String make = "";
String model = "";
String disp = "";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://xxxxxxxxxxxxxx/upload.php";
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + myfile)));
try {
FileInputStream fileInputStream = new FileInputStream(new File(myfile));
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs
connection.setDoInput(true);
// Allow Outputs
connection.setDoOutput(true);
// Don't use a cached copy.
connection.setUseCaches(false);
// Use a post method.
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outStream = new DataOutputStream(connection.getOutputStream());
outStream.writeBytes(twoHyphens + boundary + lineEnd);
outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + myfile +"\"" + lineEnd);
outStream.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
outStream.writeBytes(lineEnd);
outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
outStream.flush();
outStream.close();
}
catch (MalformedURLException ex) {
ex.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream( connection.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
disp = disp + str;
}
inStream.close();
}
catch (IOException ioex){
ioex.printStackTrace();
}
return disp;
}
You should move the code to read the response from the server to a new thread. Ex:
private class ReadResponse implements Runnable {
public void run() {
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream( connection.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
disp = disp + str;
}
inStream.close();
}
catch (IOException ioex){
ioex.printStackTrace();
}
//return disp;
//here you need to show your display on UI thread
}
}
}
and start the reading thread before uploading the file.
这篇关于开场DataInputStream以运行速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!