[英]how to Upload File using Jersey
I'm trying to create service for uploading files. 我正在尝试创建用于上传文件的服务。
I follow the guide https://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/ 我遵循指南https://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/
but when i'm trying to run i get 但是当我尝试跑步时
Status Code:405 Method Not Allowed
what i'm doing wrong? 我做错了什么?
here is my code 这是我的代码
server 服务器
@Path("/doc")
public class DocResource extends BaseResource<DocDao, DocEntity>
{
@POST
@Path("/uploadDoc")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(@Context HttpServletRequest req,
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
// save the file to the server
saveFile(fileInputStream, filePath);
String output = "File saved to server location : " + filePath;
return output;
}
// save uploaded file to a defined location on the server
private void saveFile(InputStream uploadedInputStream,
String serverLocation) {
try {
OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
HTML 的HTML
<div class="modal fade" id="addEditDoc" tabindex="-1" role="dialog" aria-labelledby="addEditDoc" data-backdrop="false" data-keyboard="false">
<div class="modal-dialog addEditDocModal" role="document">
<div class="modal-content myModal">
<h1>Upload a File</h1>
<form action="http://127.0.0.1:8080/maintenance/uploadDoc" method="GET" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" size="50" />
</p>
<input type="submit" value="Upload It" />
</form>
</div>
</div>
</
I think the method must be POST instead of GET 我认为方法必须是POST而不是GET
Please find the further Documentation Here 请在此处找到更多文档
<div class="modal-dialog addEditDocModal" role="document">
<div class="modal-content myModal">
<h1>Upload a File</h1>
<form action="http://127.0.0.1:8080/maintenance/doc/uploadDoc" method="POST" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" size="50" />
</p>
<input type="submit" value="Upload It" />
</form>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.