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 ...
随机推荐
- CSS 负边距读后感
最近看到一篇讲解CSS 负边距的文章: http://segmentfault.com/a/1190000003750411?utm_source=Weibo&utm_medium=share ...
- CentOS 7与 Windows双系统丢失Windows启动项及默认启动项修改
1.Windows启动项消失的原因: 在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为grub2,默认的CentOS7无法识别Wind ...
- 前端jQuery之动画操作及相关演示
1.显示动画 1.1无参数,直接让指定的元素显示出来 $("div").show(); 1.2通过控制宽高,透明度,display属性逐渐显示,指定时间现实完毕 $('div'). ...
- virtual base classes
virtual base classes用来实现菱形继承解决多个重复subobject的问题 //: C09:VirtualBase.cpp // Shows a shared subobject v ...
- Servlet学习笔记02——什么是http协议?
1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...
- ubuntu16 升级pip3后报错File "/usr/bin/pip3", line 9, in <module> from pip import main ImportError: cannot import name 'main'
问题:ubuntu16 执行pip3 install --upgrade pip之后,pip3执行出错. Traceback (most recent call last): File "/ ...
- Shell学习——数组
1.普通数组:只能用整数作为索引1.1.赋值[root@client02 ~]# array[0]=test1[root@client02 ~]# array[1]=test2[root@client ...
- 【HTML】placeholder中换行
表示回车 表示换行 案例 <textarea rows="10" placeholder="测试换行 新的一行"></textarea>
- C++高级 STL——模板函数、模板类
1.模板函数 // 定义 template <class T> Max(T &t1, T &t2) { return ((t1 > t2) ? t1 : t2); } ...
- oracle11g导出表时空表导不出解决方案
oracle11g用exp命令导出数据库表时,有时会发现只导出了一部分表时而且不会报错,原因是有空表没有进行导出,之前一直没有找到方法于是用最笨的方法重新建这些空表,当然在我们实际当中表的数量大时我们 ...