我是restful webservice的新手。我的要求是上传一个多个文件。我成功编写了用于在宁静的网络服务中上传多个文件的代码。这是我的代码。
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
@Path("/multipleFiles")
public String registerWebService(@Context HttpServletRequest request)
{
String responseStatus = SUCCESS_RESPONSE;
String candidateName = null;
//checks whether there is a file upload request or not
if (ServletFileUpload.isMultipartContent(request))
{
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload fileUpload = new ServletFileUpload(factory);
try
{
/*
* parseRequest returns a list of FileItem
* but in old (pre-java5) style
*/
final List items = fileUpload.parseRequest(request);
if (items != null)
{
final Iterator iter = items.iterator();
while (iter.hasNext())
{
final FileItem item = (FileItem) iter.next();
final String itemName = item.getName();
final String fieldName = item.getFieldName();
final String fieldValue = item.getString();
if (item.isFormField())
{
candidateName = fieldValue;
System.out.println("Field Name: " + fieldName + ", Field Value: " + fieldValue);
System.out.println("Candidate Name: " + candidateName);
}
else
{
final File savedFile = new File(FILE_UPLOAD_PATH + File.separator
+ itemName);
item.write(savedFile);
}
}
}
}
catch (FileUploadException fue)
{
responseStatus = FAILED_RESPONSE;
fue.printStackTrace();
}
catch (Exception e)
{
responseStatus = FAILED_RESPONSE;
e.printStackTrace();
}
}
return responseStatus;
}
在单个请求中上传多个文件是否合适? 我想使用restful模板或jersey或任何其他java客户端来使用上面的restful webservice。 任何人都可以指导我编写用于使用上述Web服务的客户端代码吗?
任何帮助将不胜感激!!!