简体   繁体   English

HTTP状态415-不支持的媒体类型:

[英]HTTP Status 415 - Unsupported Media Type:

I have a error with media type not supported in my JAX-Rs web Service program. 我的JAX-Rs Web服务程序不支持媒体类型错误。
I am following the url : http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ 我正在追踪网址: http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ : http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/
I run program and tried to upload .jpg file shows error: Media type doesnt supported. 我运行程序并尝试上传.jpg文件时显示错误:不支持媒体类型。 i am using jersey-multipart-1.17.1.jar for multipart data support. 我正在使用jersey-multipart-1.17.1.jar提供多部分数据支持。
Any body pls help I tried all. 任何身体都可以帮助我。

My WebService Program is: 我的WebService程序是:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/file")

public class UploadFileService {


    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)

    public Response uploadFile(

        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

        String uploadedFileLocation = "c://" + fileDetail.getFileName();

        // save it
        writeToFile(uploadedInputStream, uploadedFileLocation);

        String output = "File uploaded to : " + uploadedFileLocation;

        return Response.status(200).entity(output).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();
        }

    }

}

web.xml file is web.xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ExampleUpload</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
  <servlet-name>ServletAdaptor</servlet-name>
  <servlet-class>

      com.sun.jersey.server.impl.container.servlet.ServletAdaptor  

  </servlet-class>    

  <load-on-startup>1</load-on-startup>

  </servlet>
 <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>



</web-app>

and my jsp page is : 我的jsp页面是:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    <h1>File Upload with Jersey</h1>

    <form action="rest/file/upload" method="post" enctype="multipart/form-data">

       <p>
        Select a file : <input type="file" name="file" size="45" />
       </p>

       <input type="submit" value="Upload It" />
    </form>

</body>
</html>

添加mimepull.jar文件在这里解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM