Servlet中文件上传
利用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中文件上传的更多相关文章
- Servlet中文件上传下载
1.文件下载: package FileUploadAndDown; import java.io.FileInputStream; import java.io.IOException; impor ...
- Servlet中文件上传的几种方式
上传文件,因为上传的都是二进制数据,所以在Servlet中就不能直接用request.getParameter();方法进行数据的获取,需要借助第三方jar包对上传的二进制文件进行解析.常用的方式如下 ...
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- Servlet实现文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 下载地址: 1) commons-fileupload-1.2.2-bin.zip : 点击打开链接 2) commons- ...
- layUI框架中文件上传前后端交互及遇到的相关问题
下面我将讲述一下我在使用layUI框架中文件上传所遇到的问题: 前端jsp页面: <div class="layui-form-item"> <label cla ...
- Servlet实现文件上传,可多文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...
- 配置servlet支持文件上传
Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...
- easyui-dialog中文件上传处理
function openDialog() { // $('#dlg').dialog('open'); //EasyUi的dialog中文件上传,后台获取不到文件,需要改写为下面这样 $(" ...
- JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。
JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...
随机推荐
- url 解析
最近在做一个单页应用,使用AngularJS来处理一些页内路由(哈希#后的路由变化).自然会要解析URL中的参数.使用AngularJS自带的方法$location.search();可以自动将参数整 ...
- git中.gitignore 文件
现在项目的根目录放了 .gitignore 文件,并且git远程仓库的项目根目录已经有了 logs文件夹. 由于每次本地运行项目,都会生成新的log文件,但是我并不想提交logs文件夹里面的内容,所以 ...
- android动画解析(初级)
效果图: ObjectAnimator继承自ValueAnimator的,底层的动画实现机制也是基于ValueAnimator来完成的,因此ValueAnimator仍然是整个属性动画当中最核心的一个 ...
- 【c学习-5】
int main(){ //二维数组的应用 int i,j; int a[2][3]; for(i=0;i void myFunction(){ int a[3]; int i; int max; f ...
- linux防火墙的管理和策略控制
iptables 一:IPtables防火墙的简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 ...
- PHP CI框架学习
CI框架的URL辅助函数使用 URL 辅助函数文件包含一些在处理 URL 中很有用的函数 加载辅助函数 在使用CI框架的使用经常碰到跳转和路径方面的问题,site_url()和base_url()很容 ...
- java中substring()、charAt()、indexOf() (2013-05-05-bd 写的日志迁移
substring 1. public String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串, 该子字符串始于指定索引处的字符,一直 ...
- jpeglib的使用
1. 解压jpeglib tar xvzf libjpeg-turbo-1.2.1.tar.gz 2. 阅读里面的说明文件,得到jpeg解压缩的一般步骤: /*Allocate and initial ...
- 希尔排序算法Java实现
希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它的基本思想是: ...
- 笔记-HTTP及HTTPS
笔记-HTTP及HTTPS 1. HTTP 1.1. 简介 HTTP:hyper text transfer protocol 超文本传输协议 http常用请求方式,method ge ...