我目前正在使用塊上傳(Java)上傳文件到私有服務器。一切正常,但應用程序正在上傳文件時,它會凍結窗口。上傳everythin後,恢復正常,但不是在此期間。我知道它上傳,因爲它的工作原理,它顯示在控制檯的進展。所以,我試圖在不同的線程上移動所有的計算。它仍然正常運行,但凍結。此外,我還閱讀了SwingWorker,我放棄了它,但它仍然凍結了主要的GUI。Java LibGDX 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();
}
}
這裏是LibGDX屏幕,該ChunkUploader類上創建。
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衝突。有人可以告訴我我做錯了什麼嗎?
謝謝!奇蹟般有效! –
沒問題。祝你未來的事業順利。 –