我目前正在处理一个块上传器(用Java)将文件上传到私人服务器。一切都很完美,但是当应用程序上传文件时,它会冻结窗口。上传后,everythin恢复正常,但不会在期间。我知道它上传是因为它有效并且它在控制台中显示进度。所以,我试图在不同的线程上移动所有计算。它仍然运行正常,但冻结了。此外,我读到了有关SwingWorker的内容,我给了它一个去,但它仍然冻结了主GUI。
这是SwingWorker的类(所有方法都是有序的,所以你不应该迷路,每个都包含一个解释目的的注释)(这是很多代码,但你真的只需要查看BeginUpload()方法,就像循环所在的那样)
public class ChunkUploader extends SwingWorker<String, Object> {
public String Text="";
public String Text2="";
boolean Uploading=false;
private String Actual="";
private int CurrentChunk=0;
private int BytePosition=0;
private int ChunkSize=100000;
private String Contents="";
private String FileName;
private int PackageNumber;
private String ByteString;
private boolean Finalize;
private String Password;
private int TotalChunks;
private byte[] bytes;
TextPrompt Prompt;
public double Progress=0;
public boolean done=false;
public ChunkUploader(String FileName) {
//Prepares the thing
this.FileName=FileName;
Prompt = new TextPrompt();
}
@Override
protected String doInBackground() throws Exception {
//Calls the first method
PrepareUpload(FileName);
return null;
}
private void PrepareUpload(String FileName) {
//Loads the file and then prompts for a password;
if(Gdx.files.absolute(FileName).exists()) {
Actual=FileName;
System.out.println(FileName);///C:/T/Image.png
Uploading=true;
String pp[]=FileName.split("/");
this.FileName=pp[pp.length-1];
System.out.println(this.FileName);///C:/T/Forgive.txt
FileHandle h=Gdx.files.absolute(Actual);
Text = "Loading file..."+Actual;
bytes = h.readBytes();
Text2="";
Prompt.Prepare("Password", this);
Gdx.input.getTextInput(Prompt, "Input Dynamic Password:", "");
}
}
public void PromptReturn(String Return, String Text) {
//Reads the user input into the variable Password. Starts the upload
if(Return.equals("Password")) {
Password=Text;
BeginUpload();
}
}
private void BeginUpload() {
//Splits the data to chunks and encodes it Base64. Sends the data to Return();
Text = "Total size: "+Integer.toString(Contents.length());
TotalChunks=(int) Math.ceil(bytes.length/ChunkSize);
TotalChunks++;
String CurrentSend="";
CurrentChunk=0;
String All="";
Text2="Starting Upload...";
for(int i=0; i<TotalChunks; i++) {
byte[] temp=GetByteRange(CurrentChunk*ChunkSize, GetWidth(CurrentChunk*ChunkSize, bytes.length, ChunkSize));
System.out.print(Double.toString(100*BytePosition/bytes.length)+"% complete.");
Text = Double.toString(100*BytePosition/bytes.length)+"% complete.";
BytePosition=GetWidth(CurrentChunk*ChunkSize, bytes.length, ChunkSize);
CurrentSend=Base64.getEncoder().encodeToString(temp);CurrentChunk, false, Password, TotalChunks, FileName);
String p="";
while(p.isEmpty()) {
try {
p=Return(CurrentSend, CurrentChunk, false);
} catch (Exception e) {
e.printStackTrace();
}
}
All = All+CurrentSend;
CurrentChunk=CurrentChunk+1;
}
Text2="Finalizing...";
String p="";
while(p.isEmpty()) {
try {
p=Return(CurrentSend, TotalChunks, true);
} catch (Exception e) {
e.printStackTrace();
}
}
Text2="Upload should be complete.";
}
public byte[] GetByteRange(int Start, int End) {
byte[] temp = new byte[End-Start];
for(int i=Start; i<End; i++) {
temp[i-Start] = bytes[i];
}
return temp;
}
public int GetWidth(int Position, int Size, int ChunkSize) {
if(Position+ChunkSize<=Size) {
int p=ChunkSize+Position;
return p;
}
else {
return Size;
}
}
public String Return(String ByteString, int PackageNumber, boolean finalize) throws Exception {
//Sends the file data to the server.
String url = "http://www.stackoverflow.com/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "Pass="+Password+"&FileName="+FileName+"&PackageNumber="+Integer.toString(PackageNumber)+"&FinalPackageNum="+TotalChunks+"&Bytes="+ByteString;
if(finalize==true) {
urlParameters=urlParameters+"&State=Final&FinalPackageNum="+Integer.toString(TotalChunks);
}
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(urlParameters.getBytes(StandardCharsets.UTF_8));
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
con.disconnect();
System.out.println(response.toString());
System.out.println("___________________");
return response.toString();
}
}
这是创建ChunkUploader类的LibGDX屏幕。
public class FileUploadScreen implements Screen {
String FilePath;
String Text="";
String Text2="";
ChunkUploader Uploader;
OrthographicCamera camera;
MyGame game;
public FileUploadScreen(String FilePath, MyGame game) {
this.game=game;
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.FilePath = FilePath;
Uploader = new ChunkUploader(FilePath);
Uploader.execute();
}
@Override
public void render(float delta) {
Text=Float.toString(delta);
game.MainFont.setColor(Color.WHITE);
game.Screen.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();
game.batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.MainFont.draw(game.batch, Text, 100, game.Screen.y-50);
game.MainFont.draw(game.batch, Text2, 100, game.Screen.y-100);
game.batch.end();
}
}
为了更好地衡量,这里是文字提示:
public class TextPrompt implements TextInputListener {
private String Text="";
public boolean Active;
private String Return;
private ChunkUploader parent;
public TextPrompt() {
Active = false;
}
public void Prepare(String Return, ChunkUploader Parent) {
Active = true;
this.Return=Return;
this.parent=Parent;
}
@Override
public void input(String text) {
Text = text;
Active = false;
parent.PromptReturn(Return, Text);
}
@Override
public void canceled() {
Text = "";
Active = false;
}
public String GetInput() {
return Text;
}
}
同样,只要文件没有上传,一切运行正常。我只是感到困惑,因为如果它是在一个单独的线程上传,那么它不应该与GUI冲突。谁能告诉我我做错了什么?
答案 0 :(得分:0)
好吧,看起来这有一些问题。首先,需要通过doInBackground()调用ChunkUploader类中的所有方法。当您查看PromptReturn方法时,BeginUpload();被称为。
要解决这个问题,我们需要有点创意。以下是应该进行的更改:
需要创建一个可由doInBackground()和PromptReturn()访问的布尔变量。它的初始值应该是假的。
接下来,对前几个方法进行此更改:
private boolean Begin = false;
public void PromptReturn(String Return, String Text) {
//Reads the user input into the variable Password. Starts the upload
if(Return.equals("Password")) {
Password=Text;
//BeginUpload();
Begin = true;
}
}
@Override
protected String doInBackground() throws Exception {
//Calls the first method, and waits to call the second.
PrepareUpload(FileName);
boolean run=true;
while(run) {
if(Begin) {
BeginUpload()
run=false;
}
}
return null;
}
此外,值得注意的是,您使用的Base64编码器仅适用于桌面应用程序。您应该考虑切换到this Base64 encoder/decoder.它以您使用它的方式满足您当前的所有需求,并且非常容易实现。只需将Base64.java复制到您的项目中,您现在可以将它用于桌面和Android。