我已经实现了一种通过发布到Jersey JAX-RS REST后端来上传音频文件的方法。这很好用。但是,当我尝试实现DELETE音频文件方法时,它会给我以下错误:
HTTP 405 Method Not Allowed
我不能为我的生活看到什么是错的 - 我认为这可能是我无视的微不足道的事情,因为我之前做了很多DELETES并且他们已经工作了。我也试图实现一个简单的虚拟GET方法,并且也给出了同样的错误。
使用名为ng-file-upload的angularjs指令完成文件上传。 使用以下网址成功上传:
file.upload = Upload.upload({
url: 'http://localhost:8080/pododdle/webapi/auctions/' + bid.auction_id + '/uploads?category_id=' + bid.category_id,
data: {file: file}
});
上传的示例网址为:
http://localhost:8080/pododdle/webapi/auctions/1/uploads?category_id=2
我尝试使用POSTMAN来使用DELETE,因为我的前端代码是错误的。提供的网址是:
http://localhost:8080/pododdle/webapi/auctions/1/uploads
以下是整个“上传”资源的代码,包括DELETE和POST。正如您所看到的,DELETE只是一种虚拟方法,没有真正的工作。此外,上传资源是“拍卖”资源的子资源。
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UploadResource {
private static final Logger log = Logger.getLogger(UploadResource.class);
AuctionService auction_service = new AuctionService();
@DELETE
public Response deleteAudio(@PathParam("auction_id") int auction_id) {
System.out.println("inside deleteAudio");
return Response.ok().build();
}
//this uploads the audio file, stores it to disk, and then saves the file-path to the auction
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/html")
public Response uploadAudio(@PathParam("auction_id") int auction_id, @QueryParam("category_id") int category_id,
@FormDataParam("file") InputStream fileInputString, @FormDataParam("file") FormDataContentDisposition fileInputDetails) {
//We need to get the value of the audio root directory
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try {
properties.load(classLoader.getResourceAsStream("pododdle.properties"));
} catch (IOException e) {
log.error("Error getting the audio root directory location when uploading the audio: " + e.getMessage());
e.printStackTrace();
}
String auctionAudio = "audio/category/" + category_id + "/auction/" + auction_id + "/" + fileInputDetails.getFileName();
String audioFileName = properties.getProperty("audioRootDirectory") + auctionAudio;
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
try {
File audioFile = new File(audioFileName);
File parent = audioFile.getParentFile();
if(!parent.exists() && !parent.mkdirs()) {
return Response.status(501).entity("Error creating directory when uploading the audio to the server. Please try again in a few moments").build();
}
OutputStream out = new FileOutputStream(audioFile, false);
byte[] buffer = new byte[1024];
int bytes = 0;
long file_size = 0;
while((bytes = fileInputString.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
file_size += bytes;
}
out.flush();
out.close();
log.info(String.format("Uploading Audio file for category: " + category_id + ", fileSize: %s", myFormat.format(file_size)));
} catch (IOException ioe) {
System.out.println("error uploading audio file to server: " + ioe.toString());
return Response.status(501).entity("Error uploading the audio to the server. Please try again in a few moments").build();
}
//now updating the auction with the audio path in the database and return the updated auction
return Response.ok(auction_service.updateAuction(auction_id, auctionAudio).toString()).build();
}
}
拍卖资源如下所示:
@Path("/auctions")
public class AuctionResource {
//creating sub-resource for auction bids
@Path("/{auction_id}/uploads")
public UploadResource getUploadResource()
{
return new UploadResource();
}
}
答案 0 :(得分:0)
看起来这个错误是由于项目没有正确构建造成的。在检查构建路径时,似乎有两个jar文件神秘地丢失了!他们是:
jersey-media-multipart-2.21.1.jar
mimepull-1.9.3.jar
我不知道jar文件是如何从glassfish服务器的/ lib目录中删除的,也许我不应该喝那瓶额外的威士忌。一旦他们被重新加入,一切都突然变得笨拙。有点困惑的错误 - 如果罐子不可用,我会认为它会产生编译错误。无论如何,由于另一个恼人的错误而损失了5天。