我遇到了问题 我有我学校的这段代码,我需要将此代码与jfilechooser结合使用。它的工作方式应为:按下按钮以查找您要在服务器上上传的文件->在服务器上上传->获得成功答案->在GUI上显示
import static java.text.MessageFormat.format;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class BuildServerServerApplicationTests {
@LocalServerPort
int port;
@Value("${storage.rootpath}")
private String ROOT_PATH;
@Test
public void uploadFile() throws IOException {
String filePath = "C:\\temp\\build-server-test\\";
String extension = ".txt";
String zipExtension = ".zip";
String filename = "test";
String author = "johndoe";
Path zipFile = Paths.get(filePath, filename + zipExtension);
Path originalFile = Paths.get(filePath, filename + extension);
Path targetPath = Paths.get(ROOT_PATH + "\\" + filename + zipExtension);
Files.deleteIfExists(targetPath);
Files.deleteIfExists(zipFile);
ZipUtils.zip(originalFile, zipFile);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("author", author);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(zipFile.toFile()));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
String url = format("http://localhost:{0}/file/upload", String.valueOf(port));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
System.out.println(response);
assertThat(Files.exists(targetPath), is(true));
assertThat(new String((byte[])Files.getAttribute(targetPath, "user:author")), is(author));
}
}
希望您能帮助我...感谢您提供的所有建议:) 如果您需要更多信息,请告诉我,我很乐意为您提供帮助