我试图使用GlassFish服务器中用Java开发的SOAP服务将图像上传到mysql数据库。这个web服务是由JSP中的客户端使用的。我找了很多遍,但找不到合适的答案。
有人能帮我吗?提前感谢!
发布于 2015-01-21 14:22:34
您必须使用JAX或其他框架(如CXF、Axis或Spring WS.The客户端代码)创建客户端代码来使用web服务,这些客户机代码将位于应用程序的控制器中。JSP将充当视图,将数据发送到服务到控制器,然后控制器将与web服务交互。
下面是JSP和控制器的框架:
<form action="${request.contextPath}/path/to/controller" method="POST" enctype="multipart/form-data">
File to upload:
<input type="file" name="fileData" />
<br />
<!-- probably more fields, depending on your requirements... -->
<input type="submit" value="Upload file">
</form>控制器代码(由于您没有指定要使用的特定框架,所以我使用的是普通Servlet):
@WebServlet("/path/to/controller")
public class FileUploadToWSServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//consume the data from JSP
//pass the data received from JSP
//to send it to consume the JAX-WS service
}
}尝试从JSP直接使用web服务是可以通过scriptlet完成的,但是应该避免使用它,因此不推荐这种方法,也不是我的答案的一部分。
发布于 2015-01-24 21:20:34
这是我问题的全部答案。我不认为.jsp页面会有问题,您只需要创建一个包含输入的表单。处理上载的代码如下:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "";
String comment = "";
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
} else {
if ("comment".equals(item.getFieldName())) {
comment = item.getString();
// Whatever you have to do with the comment
}
}
}
addPhoto((int) request.getSession().getAttribute("id"), UPLOAD_DIRECTORY + File.separator + name , comment);
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}else{
request.setAttribute("message","Sorry this Servlet only handles file upload request");
}
request.getRequestDispatcher("/index.jsp").forward(request, response);
}https://stackoverflow.com/questions/28069418
复制相似问题