【发布时间】:2014-05-06 14:50:54
【问题描述】:
我正在尝试上传 png 图像(然后从另一个客户端下载)。 我收到 Http 状态代码 415:我的错误是什么?
我已经在这里和谷歌上阅读了所有内容,但仍未解决。
这是我的服务器代码:
@Path("/file")
public class UploadFileService {
@POST
@Path("/upload")
@Consumes("image/png")
public Response uploadPng(File file) throws IOException {
String uploadedFileLocation =
"C:/Users/Desktop/server/" + file.getName();
DataInputStream diStream =
new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead =
diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
// save it
writeToFile(diStream, uploadedFileLocation);
System.out.println("File uploaded to : " + uploadedFileLocation);
return Response.status(200).entity(file).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out =
new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是客户端代码:
public class FileUploadClient {
public static void main(String[] args) {
try {
File fileToUpload = new File("C:/Users/client/file.png");
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource =
client.resource("http://localhost:8080/WS/rest/file/upload");
ClientResponse response =
resource.accept("image/png").post(ClientResponse.class, fileToUpload);
System.out.println(response.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
}
}
编辑 它的工作原理是这样的:
public class FileUploadClient{
public static void main(String[] args){
try{
File fileToUpload = new File("C:/Users/client/file.png");
InputStream is =new FileInputStream(fileToUpload);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource("http://localhost:8080/WS/rest/");
@SuppressWarnings("resource")
FormDataMultiPart part = new FormDataMultiPart().
field("file", is, MediaType.APPLICATION_OCTET_STREAM_TYPE);
String response = resource.path("file").path("upload").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
}
catch(Exception e){
e.printStackTrace();
}
}
}
这是 POST。
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream){
//@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "C:/Users/Desktop/server/file.png";
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
【问题讨论】:
-
尝试使用不同的内容类型。还有一张不同的图片。
-
使用不同的 png 图像或 ("image/jpeg") 和 .jpg 图像没有任何变化。
-
@Schiawo 你如何执行你的代码来上传文件?
-
对不起,我不明白你的问题。我在 Eclipse 上做了一个像 youtube.com/watch?v=qmEVsUJh2d8 这样的动态 Web 项目,然后我运行你在 EDIT 之后看到的 main 方法。
标签: java eclipse web-services rest jersey