IOException:使用java resteasy-multipart-provider时访问被拒绝

时间:2014-11-20 21:35:42

标签: java resteasy

当我从index.jsp提交表单时,我的服务URL unhandle java.lang.RuntimeException:java.io.IOException:访问被拒绝。为什么@MultipartForm无法理解或无法访问MyMultipartForm?

这是代码:

的index.jsp:

<html>
 <body>
  <form method="POST" action="services/author/test2" enctype="multipart/form-data">
    <p>
        Choose the file : <input type="file" name="file" />
    </p>
    <input type="submit" value="Upload" />
  </
 </body>
</html>

MyMultipartForm.java:

import java.io.InputStream;

import javax.ws.rs.FormParam;

import org.jboss.resteasy.annotations.providers.multipart.PartType;

public class MyMultipartForm {


@FormParam("file")
@PartType("image/jpeg")
private InputStream file_input;

public InputStream getFile_input() {
    return file_input;
}

public void setFile_input(InputStream file_input) {
    this.file_input = file_input;
}

}

休息服务:

private static final String SERVER_UPLOAD_LOCATION_FOLDER = "E://temp2/";
@POST
@Path("test2")
@Consumes("multipart/form-data")
public Response uploadFile1(@MultipartForm MyMultipartForm form) {

    String fileName = SERVER_UPLOAD_LOCATION_FOLDER + "randomName.jpg";

    saveFile(form.getFile_input(), fileName);

    String output = "File saved to server location : " + fileName;

    return Response.status(200).entity(output).build();

}

// 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();
    }
 }

我在Intellij Idea 13.1.5中使用了tomcat7,jdk1.7的maven webapp项目。

0 个答案:

没有答案