文件的上传
文件域和表单的enctype属性
- HTML<form>标签enctype属性
application/x-www-form-urlencoded:在发送前编码所有字符(默认)
multipart/form-data: 不对字符编码,或在使用包含文件上传控件的表单时,必须使用该值。
text/plain:空格转换为 “+” 加号,但不对特殊字符编码。 - enctype
规定了form表单在发送到服务器时候编码方式,有如下的三个值。
1、application/x-www-form-urlencoded。默认的编码方式。但是在用文本的传输和MP3等大型文件的时候,使用这种编码就显得 效率低下。
2、multipart/form-data 。 指定传输数据为二进制类型,比如图片、mp3、文件。
3、text/plain。纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码。 - 示例
<fieldset>
<legend>上传单个文件</legend>
<form action="upload" method="post" enctype="multipart/form-data">
上传文件: <input type="file" name="myFile"><br>
<input type="submit" value="上传">
</form>
</fieldset>
<fieldset>
<legend>上传多个文件</legend>
<form action="upload" method="post" enctype="multipart/form-data">
上传文件: <input type="file" name="myFile"><br>
上传文件: <input type="file" name="myFile"><br>
<input type="submit" value="上传">
</form>
</fieldset>
@MultipartConfig注解和Part对象
Servlet3.0 提供了@MultipartConfig注解,让我们的servlet能够接受来自客户端提交的二进制数据,通过request对象的getPart/getParts方法即可从请求中提取上传的文件信息
- 示例
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Collection<Part> partList = req.getParts(); //获取当前表单中提交的所有文件,返回一个集合
Part part = req.getPart("name"); //根据文件域所对应的name提取单个文件
}
}
文件的下载
要实现下载非常简单,只需要向浏览器声明content-disposition标明attachment即可,如果不标识这个信息,浏览器会直接打开该文件(如果浏览器可以打开,如图片,文档等),如果打不开也会激活下载大致思路就是,我们通过一个输入流,读取想要让客户端下载的文件字节,然后通过输出流写到客户端即可,基本的IO操作
@WebServlet("/down")
public class DownLoadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
// String fileName = new String(req.getParameter("fileName").getBytes("iso-8859-1"),"utf-8");
String fileName =req.getParameter("fileName");
System.out.println(fileName);
String path = req.getServletContext().getRealPath("/upload");
File f= new File(path+File.separator+fileName);
resp.setCharacterEncoding("utf-8");
resp.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
System.out.println(f);
try{
InputStream input = new FileInputStream(f);
ServletOutputStream out = resp.getOutputStream();
byte[] buffer = new byte[1024];
int len =0;
while((len=input.read(buffer))!=-1){
out.write(buffer);
}
input.close();
out.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}