java rest 图片上传_使用Java中的Rest API上传文件

本文介绍了一个新手如何使用Java REST API实现文件上传。通过一个包含文本框和文件选择器的HTML表单,用户可以指定文件路径并上传文件。在Java的UploadFileService类中,文件被保存到用户指定的路径。然而,代码存在一个问题导致HTTP 500错误,可能原因是尝试在非表单数据类型请求中使用@FormParam。
摘要由CSDN通过智能技术生成

我是REST API的新手。我想使用REST

API将用户选择的文件上传到用户提供的路径(远程或本地路径)。我的html文件具有1个文本框和1个文件选择器。用户将在文本框中输入FilePath(本地或远程计算机文件夹位置)。请提出如何解决此问题的建议。

这是我的代码:

FileUpload.html ::

Select a file :

Target Upload Path :

UploadFileService.java

@Path("/file")

public class UploadFileService {

@POST

@Path("/upload")

@Consumes(MediaType.MULTIPART_FORM_DATA)

public Response uploadFile(

@FormDataParam("file") InputStream uploadedInputStream,

@FormDataParam("file") FormDataContentDisposition fileDetail,

@FormParam("path") String path) {

/*String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();*/

/*String uploadedFileLocation = //10.217.14.88/Installables/uploaded/" + fileDetail.getFileName();*/

String uploadedFileLocation = path

+ 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

JAXRSFileUploadJerseyExample

jersey-serlvet

com.sun.jersey.spi.container.servlet.ServletContainer

com.sun.jersey.config.property.packages

com.mkyong.rest

1

jersey-serlvet

/rest/*

例外:

HTTP Status 500 - com.sun.jersey.api.container.ContainerException: Exception obtaining parameters

type Exception report

message com.sun.jersey.api.container.ContainerException: Exception obtaining parameters

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.sun.jersey.api.container.ContainerException: Exception obtaining parameters

com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:425)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)

javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

com.sun.jersey.api.container.ContainerException: Exception obtaining parameters

com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:54)

com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:125)

com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)

com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:203)

com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)

com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)

com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)

com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)

com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)

com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)

com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)

com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)

com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)

javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded

com.sun.jersey.server.impl.model.parameter.FormParamInjectableProvider$FormParamInjectable.getValue(FormParamInjectableProvider.java:81)

com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)

com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:125)

com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)

com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:203)

com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)

com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)

com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)

com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)

com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)

com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)

com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)

com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)

com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)

javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值