六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 138|回复: 0

servlet3.0的学习(三)

[复制链接]

升级  44%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-2-7 19:34:04 | 显示全部楼层 |阅读模式
文件的上传
Upload.java
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.UUID;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;@WebServlet(urlPatterns = {"/upload"})public class Upload extends HttpServlet {    @Override    public void init(ServletConfig config) throws ServletException {        super.init(config);    }    @Override    protected void service(HttpServletRequest req, HttpServletResponse res)            throws IOException, ServletException {        PrintWriter out = res.getWriter();        InputStream is = null;        FileOutputStream fos = null;        String targetDirectory = req.getServletContext().getRealPath("/upload");        try {            for (Part p : req.getParts()) {                UUID id = UUID.randomUUID();                out.write("Part name: " + p.getName() + "<br/>\n");                out.write("Size: " + p.getSize() + "<br/>\n");                out.write("Content Type: " + p.getContentType() + "<br/>\n");                is = p.getInputStream();                File file = new File(targetDirectory, id.toString() + ".jpg");                fos = new FileOutputStream(file);                out.write("Header Names:");                for (String name : p.getHeaderNames()) {                    out.write(" " + name);                }                out.write("<br/><br/>\n");                while (true) {                    int bytedata = is.read();                     if (bytedata == '\n') out.write("<br/>");                    if (bytedata == -1) {                        break;                    }                    fos.write(bytedata);                }            }        } finally {            if (is != null) {                is.close();            }            if (out != null) {                out.close();            }        }    }}

index.html
<html><head>  <title>File Upload Example</title></head><body>  <form action="upload" method="post"        enctype="multipart/form-data">    <h3>Choose files to upload.</h3>    <input name="myFile" type="file" /> <br/>    <input name="myFile2" type="file"/> <br/> <br/>    <input value="upload" type="submit"/>    <input type="reset"/> <br/>  </form></body></html>

讲解:javax.servlet.http.Interface Part,这个接口主要是用来接收提交形式为multipart/form-data POST 请求.
void delete() :删除一个文件项目的底层存储,包括删除任何相关的临时磁盘文件
java.lang.String getContentType():获取内容类型,如image/x-png,
application/octet-stream
java.lang.String getHeader(java.lang.String name):返回指定的MIME头
java.util.Collection<java.lang.String> getHeaderNames():获取本部分的头名。
java.io.InputStream getInputStream():返回当前文件的输入流
java.lang.String getName():获取文件名,input的name
long getSize():获取大小
void write(java.lang.String fileName):写入本地硬盘,如果有多个文件需要保存时次方法并不能保证成功
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表