【问题标题】:how to Upload File using Jersey如何使用 Jersey 上传文件
【发布时间】:2016-11-02 14:42:36
【问题描述】:

我正在尝试创建上传文件的服务。

我按照指南https://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/

但是当我尝试跑步时,我得到了

Status Code:405 Method Not Allowed

我做错了什么?

这是我的代码

服务器

@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

<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>    
</

【问题讨论】:

    标签: java jersey jetty


    【解决方案1】:

    我认为方法必须是 POST 而不是 GET

    请查找更多文档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> 
    

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2014-09-03
      相关资源
      最近更新 更多