博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java EE之通过表单上传文件
阅读量:6265 次
发布时间:2019-06-22

本文共 3407 字,大约阅读时间需要 11 分钟。

public class Ticket{    private String customerName;    private String subject;    private String body;    private Map
attachments = new LinkedHashMap<>(); //...}
public class Attachment{    private String name;    private byte[] contents;    //...}
@WebServlet(        name = "ticketServlet",        urlPatterns = {"/tickets"},        loadOnStartup = 1)@MultipartConfig(        fileSizeThreshold = 5_242_880, //5MB        maxFileSize = 20_971_520L, //20MB        maxRequestSize = 41_943_040L //40MB)//@MultipartConfig的location告诉浏览器应该在哪里存储临时文件(这里未使用,让应用服务器使用它的默认临时目录即可)//fileSizeThreshold将告诉Web容器文件必须达到多大才能写入到临时目录中(在本例中,小于5MB的上传文件将保存在内存中。)//在本例中,maxFileSize设置将禁止上传大小超过20MB的文件。//在本例中,maxRequestSize设置禁止大小超过40MB的请求。public class TicketServlet extends HttpServlet{    //...}
public class TicketServlet extends HttpServlet{    //...        //downloadAttachment方法用于处理客户端浏览器的下载请求。    private void downloadAttachment(HttpServletRequest request,                                    HttpServletResponse response)            throws ServletException, IOException    {        //...        //响应中设置的头Content-Disposition,将强制浏览器询问客户是        //保存还是下载文件,而不是在浏览器中在线打开该文件。        response.setHeader("Content-Disposition",                "attachment; filename=" + attachment.getName());        response.setContentType("application/octet-stream");        //使用ServletOutputStream将文件内容输出到响应中(对大文件处理存在内存问题)        ServletOutputStream stream = response.getOutputStream();        stream.write(attachment.getContents());    }    //...}
public class TicketServlet extends HttpServlet{    //...        private void createTicket(HttpServletRequest request,                              HttpServletResponse response)            throws ServletException, IOException    {        Ticket ticket = new Ticket();        ticket.setCustomerName(request.getParameter("customerName"));        ticket.setSubject(request.getParameter("subject"));        ticket.setBody(request.getParameter("body"));        Part filePart = request.getPart("file1");        if(filePart != null && filePart.getSize() > 0)        {            Attachment attachment = this.processAttachment(filePart);            if(attachment != null)                ticket.addAttachment(attachment);        }        int id;        synchronized(this)        {            id = this.TICKET_ID_SEQUENCE++;            this.ticketDatabase.put(id, ticket);        }        response.sendRedirect("tickets?action=view&ticketId=" + id);    }    private Attachment processAttachment(Part filePart)            throws IOException    {        //从multipart请求中获得InputStream,并将它复制到Attachment对象中。        InputStream inputStream = filePart.getInputStream();        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();        int read;        final byte[] bytes = new byte[1024];        while((read = inputStream.read(bytes)) != -1)        {            outputStream.write(bytes, 0, read);        }        Attachment attachment = new Attachment();        attachment.setName(filePart.getSubmittedFileName());        attachment.setContents(outputStream.toByteArray());        return attachment;    }    //...}

文章的源码链接:https://pan.baidu.com/s/1ZnkwhlZo6_ihutdbgA7FvQ 密码:q33p

其他关于文件上传的源码链接:https://pan.baidu.com/s/1PW-sdelXD6i3S3deFmVi0g 密码:v7hd
《Java Web高级编程》示例代码的下载地址:http://www.wrox.com/go/projavaforwebapps

参考:《Java Web高级编程》第3章 第6节

转载于:https://www.cnblogs.com/gzhjj/p/9134178.html

你可能感兴趣的文章
20172303 2018-2019-1 《程序设计与数据结构》第3周学习总结
查看>>
[Javascript]史上最短的IE浏览器判断代码
查看>>
关于《大道至简》第五章的收获
查看>>
网卡驱动
查看>>
kinect sdk开发入门WPFdemo笔记
查看>>
Server.Transfer详细解释
查看>>
java单链表的增、删、查操作
查看>>
The working copy at 'xxx' is too old 错误解决
查看>>
jadclipse
查看>>
// 1.什么是JOSN?
查看>>
SQL语句详细汇总
查看>>
如何保护.net中的dll文件(防破解、反编译)
查看>>
Python 装饰器
查看>>
Docker 网络模式
查看>>
[POI2013]Usuwanka
查看>>
problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 算法分析...
查看>>
nodejs + CompoundJS 资源
查看>>
转:C#并口热敏小票打印机打印位图
查看>>
scau 17967 大师姐唱K的固有结界
查看>>
spring之<bean>实例化
查看>>