首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JSP客户端使用JAX将图像上传到mysql数据库?

如何使用JSP客户端使用JAX将图像上传到mysql数据库?
EN

Stack Overflow用户
提问于 2015-01-21 14:18:48
回答 2查看 303关注 0票数 0

我试图使用GlassFish服务器中用Java开发的SOAP服务将图像上传到mysql数据库。这个web服务是由JSP中的客户端使用的。我找了很多遍,但找不到合适的答案。

有人能帮我吗?提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-21 14:22:34

您必须使用JAX或其他框架(如CXF、Axis或Spring WS.The客户端代码)创建客户端代码来使用web服务,这些客户机代码将位于应用程序的控制器中。JSP将充当视图,将数据发送到服务到控制器,然后控制器将与web服务交互。

下面是JSP和控制器的框架:

代码语言:javascript
复制
<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):

代码语言:javascript
复制
@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完成的,但是应该避免使用它,因此不推荐这种方法,也不是我的答案的一部分。

票数 -1
EN

Stack Overflow用户

发布于 2015-01-24 21:20:34

这是我问题的全部答案。我不认为.jsp页面会有问题,您只需要创建一个包含输入的表单。处理上载的代码如下:

代码语言:javascript
复制
@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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28069418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档