如何使用JSP -Servlet和EJB 3.0上载映像 [英] How to upload an image using JSP -Servlet and EJB 3.0

查看:128
本文介绍了如何使用JSP -Servlet和EJB 3.0上载映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JSP Servlet和ejb 3.0上传图像

I want to upload an Image using JSP Servlet and ejb 3.0

推荐答案

首先,选择要上传的文件JSP至少需要一个HTML < input type =file> 元素,它将显示一个文件浏览字段。如您需要 HTML表单规范中所述将请求方法设置为 POST ,并将请求编码设置为父<$ c $中的 multipart / form-data c>< form> 元素。

To start, to select a file for upload using JSP you need at least a HTML <input type="file"> element which will display a file browse field. As stated in the HTML forms spec you need to set the request method to POST and the request encoding to multipart/form-data in the parent <form> element.

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

因为在 Servlet 3.0 (我不认为你使用的是因为EJB 3.0是Java EE 5.0的一部分,而Java EE 5.0又包含Servlet 2.5,您在请求参数映射中看不到任何内容。 request.getParameter(file)将返回 null

Because the aforementioned request encoding isn't by default supported by the Servlet API before Servlet 3.0 (which I don't think you're using because EJB 3.0 is part of Java EE 5.0 which in turn contains Servlet 2.5 only), you won't see anything in the request parameter map. The request.getParameter("file") would return null.

要在servlet中检索上传的文件和其他请求参数,需要解析 InputStream HttpServletRequest 你自己。幸运的是,有一个常用的API可以从您手中完成繁琐的工作: Apache Commons FileUpload

To retrieve the uploaded file and the other request parameters in a servlet, you need to parse the InputStream of the HttpServletRequest yourself. Fortunately there's a commonly used API which can take the tedious work from your hands: Apache Commons FileUpload.

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
    if (!item.isFormField()) {
        // <input type="file">
        System.out.println("Field name: " + item.getFieldName());
        System.out.println("File name: " + item.getName());
        System.out.println("File size: " + item.getSize());
        System.out.println("File type: " + item.getContentType());
    } else {
        // <input type="text|submit|hidden|password|button">, <select>, <textarea>, <button>
        System.out.println("Field name: " + item.getFieldName());
        System.out.println("Field value: " + item.getString());
    }            
}

基本上你只需要获得来自 FileItem 对象的InputStream 并根据您的喜好将其写入任何 OutputStream 通常的 Java IO 方式。

Basically you just need to get the InputStream from the FileItem object and write it to any OutputStream to your taste using the usual Java IO way.

InputStream content = item.getInputStream();

或者您也可以直接书写:

Alternatively you can also write it directly:

item.write(new File("/uploads/filename.ext"));

在他们的主页上,您可以找到很多代码示例以及 用户指南 常见问题 部分。仔细阅读它们。

At their homepage you can find lot of code examples and important tips&tricks in the User Guide and Frequently Asked Questions sections. Read them carefully.

这篇关于如何使用JSP -Servlet和EJB 3.0上载映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆