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 ...
随机推荐
- C#浏览器中在线操作文档
源码地址:https://github.com/SeaLee02/FunctionModule 文件夹 UploadFiles/WebDemo/COM/OnlineEdit.aspx 就是源码 用 ...
- 修改SVN的地址
方法 1:右键在工作复本的根目录上右键->TortoiseSVN->重新定位 (Relocate),然后修改URL就可以了,但最好先备份一下,据说这样的操作有一定的危险性(至今我没有发现) ...
- LeetCode970. 强整数
问题:970. 强整数 用户通过次数0 用户尝试次数0 通过次数0 提交次数0 题目难度Easy 给定两个非负整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 ...
- ethereum(以太坊)(八)--Address
pragma solidity ^0.4.0; contract Test{ address _owner; uint160 _c; constructor() public{ _owner = 0x ...
- 【转载】VS2015 + EF6连接MYSQL5.6
引用文章:https://jingyan.baidu.com/article/ce09321b9cc43f2bff858fbf.html 安装包注意点说明: 1.程序名称:mysql-for-visu ...
- thinkPHP5.0 save和saveAll,新增和更新的问题
今天遇到一个问题,在模型中使用save保存数据之后,使用saveAll继续新增数据,结果报 缺少更新条件,网上搜了下发现一篇文章https://www.jianshu.com/p/1848f61de6 ...
- linuxC编程介绍
第一步:写完程序 /first.c/ #include <stdio.h> int main() { printf("hello,welcome to the LinuxC!\n ...
- JQ实现下拉加载更多
var x=0; var isloading=0; function getUsersLimited(data) { list = list.concat(data); buildList(list) ...
- 关于C、内存、栈的一些杂谈
c的程序要手动管理内存的,所有的数据(结构)都可以分为两种存储方式,连续存储,顾名思义申请一片连续的内存以供使用(数组.结构体.共用体.栈.队列):非连续存储,顾名思义断断续续的的存储,那有一点这有一 ...
- 20,Django contenttypes 应用
contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. 1.创建一个项目 2.数据库迁移,生成默认表. 3.存着所 ...