利用getReader()和getInputstream()上传

package control;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*; @WebServlet("/fileupload")
public class Fileupload extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//request.setCharacterEncoding("utf-8");
//方法一
/* ServletInputStream sis=request.getInputStream();
byte[] tmp=new byte[8192];
File file=new File("E:\\dc.zip");
FileOutputStream fos=new FileOutputStream(file);
int i=sis.read(tmp);
while (i!=-1){
fos.write(tmp,0,i);
i=sis.read(tmp);
}
fos.close();*/
//方法二
BufferedReader br=request.getReader();
/*File file=new File("E:\\da.doc");
BufferedWriter bw=new BufferedWriter(new FileWriter(file));*/
String s=br.readLine();
while (s!=null){
System.out.println(s);
s=br.readLine();
}
br.close();
PrintWriter out=response.getWriter();
out.write("success");
out.close();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}

Servlet3.0中Part,getPart()文件上传

package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
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;
import java.io.IOException;
@MultipartConfig(location = "e:\\",maxFileSize = 1024*1024*2)//保存文件路径
@WebServlet("/ServletFile")
public class ServletFile extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Part part=request.getPart("file");
String head=part.getHeader("Content-Disposition");//获取文件头Content-Disposition
System.out.println(head);
//获取文件名
int start=head.lastIndexOf("=\"");
String filename=head.substring(start+2);
filename=filename.substring(0,filename.length()-1);
System.out.println(filename);
part.write(filename);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}

多文件上传getParts()就是把单文件用foreach()处理

//多文档上传
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
for (Part part:request.getParts()){
String head=part.getHeader("Content-Disposition");//获取文件头Content-Disposition
System.out.println(head);
//获取文件名
int start=head.lastIndexOf("=\"");
String filename=head.substring(start+);
filename=filename.substring(,filename.length()-);
System.out.println(filename);
part.write(filename);

Servlet中文件上传的更多相关文章

  1. Servlet中文件上传下载

    1.文件下载: package FileUploadAndDown; import java.io.FileInputStream; import java.io.IOException; impor ...

  2. Servlet中文件上传的几种方式

    上传文件,因为上传的都是二进制数据,所以在Servlet中就不能直接用request.getParameter();方法进行数据的获取,需要借助第三方jar包对上传的二进制文件进行解析.常用的方式如下 ...

  3. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  4. Servlet实现文件上传

    一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip      :   点击打开链接 2) commons- ...

  5. layUI框架中文件上传前后端交互及遇到的相关问题

    下面我将讲述一下我在使用layUI框架中文件上传所遇到的问题: 前端jsp页面: <div class="layui-form-item"> <label cla ...

  6. Servlet实现文件上传,可多文件上传

    一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...

  7. 配置servlet支持文件上传

    Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...

  8. easyui-dialog中文件上传处理

    function openDialog() { // $('#dlg').dialog('open'); //EasyUi的dialog中文件上传,后台获取不到文件,需要改写为下面这样 $(" ...

  9. JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。

    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...

随机推荐

  1. js判断移动端还是PC端

    function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/i ...

  2. mask r-cnn

    mask R-cnn, kaiming he的新作.可以同时完成object detection和segmentation,还可以做pose estimation,简直就是功能多多啊.在coco上测试 ...

  3. 树梅派3B kali2.0 启用SSH进行远程登录

    工具/原料 kali 2.0 ssh SSH连接工具(XShell)or PUTTY vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉, ...

  4. poj_1845_Sumdiv

    Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...

  5. mysql基础 日期类型

  6. 给树莓派Raspbian stretch版本修改软件源

    树莓派最新的系统版本是stretch,试了阿里和网易的软件源都不行,最后试了清华的可以 deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbia ...

  7. java 微信开发 常用工具类(xml传输和解析 json转换对象)

    与微信通信常用工具(xml传输和解析) package com.lownsun.wechatOauth.utl; import java.io.IOException; import java.io. ...

  8. tcl之array操作

  9. php图片压缩-高清晰度

    php高清晰度无损压缩 经常会用到把上传的大图片压缩,特别是体积,在微信等APP应用上,也默认都是有压缩的,那么,怎么样对图片大幅度压缩却仍能保持较高的清晰度呢? 压缩通常是有按比例缩放,和指定宽度压 ...

  10. poj 3111 卖珠宝问题 最大化平均值

    题意:有N件分别价值v重量w的珠宝,希望保留k件使得 s=v的和/w的和最大 思路:找到贡献最大的 设当前的s为mid(x) 那么贡献就是 v-w*x 排序 ,取前k个 bool operator&l ...