java

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Part part = req.getPart("file");
String cd = part.getHeader("content-disposition");
String filename=req.getParameter("filename");
InputStream is=part.getInputStream();
File file=new File("d:"+File.separatorChar, filename);
FileOutputStream fos=new FileOutputStream(file);
byte[] buff=new byte[*];
int len=;
while((len=is.read(buff))>) {
fos.write(buff, , len);
fos.flush();
}
fos.close();
is.close();
}

js

<script src="./js/jquery-3.3.1.js"></script>

<script>
$(function(){
$("input[type='submit']").click(
function(){
var filepath=$("input[type='file']").val();
var file=filepath.split("\\");
var filename=file[file.length-];
$("input[type='hidden']").attr("value",filename);
});
});
</script>
</head>
<body>
<h1>hello tomcat .....</h1></br>
<form action="upfile" method="post" enctype="multipart/form-data"> <input type="file" name="file"/>
<input type="hidden" name="filename"/>
<input type="submit" value="submit" name="submit"/> </form>
</body>
</html>

servlet 上传文件的更多相关文章

  1. 使用Servlet上传文件

    使用浏览器向服务器上传文件其本质是打开了一个长连接并通过TCP方式传输数据.而需要的动作是客户端在表单中使用file域,并指定该file域的name值,然后在form中设定enctype的值为mult ...

  2. Servlet上传文件

    Servlet上传文件 1.准备工作 (1)利用FileUpload组件上传文件,须要到apache上下载commons-fileupload-1.3.1.jar 下载地址:http://common ...

  3. 原生Servlet 上传文件

    依赖jar <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons ...

  4. java servlet上传文件并把文件内容显示在网页中

    servlet3.0(JDK1.6)自带的API即可实现本地文件的上传,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象.下面我 ...

  5. servlet上传文件报错(三)

    1.具体报错如下 null null Exception in thread "http-apr-8686-exec-5" java.lang.OutOfMemoryError: ...

  6. 5.servlet 上传文件

    一.maven依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>com ...

  7. JSP && Servlet | 上传文件

    在WebContent下新建index.jsp 要点: 1.  表单 method 属性应该设置为 POST 方法,不能使用 GET 方法. 2.  表单 enctype 属性应该设置为 multip ...

  8. J2EE:Servlet上传文件到服务器,并相应显示

    Servlet 可以与HTML一起使用来允许用户上传文件到服务器 编辑上传文件的页面upload.html 注意事项:上传方式使用POST不能使用GET(GET不能上传文件) 表单 enctype 属 ...

  9. servlet上传文件报错(二)

    1.具体报错如下: java.io.FileNotFoundException: D:\MyEclipse\workspace\FileUpload\WebRoot\upload (拒绝访问.) at ...

  10. JAVA servlet 上传文件(commons-fileupload, commons-io)

    <1>获取二进制文件流并输出 InputStream inputStream = request.getInputStream(); BufferedReader reader = new ...

随机推荐

  1. No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

  2. Mount error(5):Input/output error on mount

    https://superuser.com/questions/850301/mount-error5input-output-error-on-mount When setting up a sha ...

  3. VS2019企业版产品密钥

    Visual Studio 2019 Enterprise产品密钥(激活码) BF8Y8-GN2QH-T84XB-QVY3B-RC4DF

  4. UVA 11375 高精度Bign类

    求火柴的组成的数字最多能组成多少种数字,典型的递推问题 但是因为结果巨大,要用高精度运算 一开始手写高精度,不仅挫的要死,最后还WA了. 最后学了一下白书上面的bign类,相当方便啊. #includ ...

  5. String StringBuffer和StringBuilder的区别和联系

    1:String,StringBuffer和StringBuilder概念 1.1:String String中使用字符串数组来存储字符串,但是是fianl来修饰的,所以String的内容不可改变. ...

  6. Python pip安装时You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

    在我们安装第三方库的时候会在结尾出现如下两行内容 You are using pip version 9.0.1, however version 18.0 is available. You sho ...

  7. NiFi_Demo_调度示例

    1.背景 要求:每天凌晨1:00后开始每2min执行一次sql查询 2.作业编排 3.各模块配置 3.1 GenerateFlowFile 作用:用于产生flowfile,该flowfile内容为空. ...

  8. 1月18日 LCA专项训练

    A. Lorenzo Von Matterhorn B.Minimum spanning tree for each edge C.Misha, Grisha and Underground D.Fo ...

  9. UML-逻辑架构精化

    向下请求:Facade模式 向上返回:观察者模式 不局限于上图中指定的层使用相应模式,其他层也可以使用. 另外,尽量不要出现“公共业务模块”,设计时尽量做好系统拆分.否则,一旦修改公共代码,可能会影响 ...

  10. python pandas 画图、显示中文、股票K线图

    目录: 1.pandas官方画图链接 2.标记图中数据点 3.画图显示中文 4.画股票K线图 5.matplotlib基本用法 6.format输出 6.format输出例子 eps_range=[0 ...