How do I upload a file to my servlet or JSP?
On the client side, the client's browser must support form-based upload. Most modern browsers do, but there's no guarantee. For example,
<FORM ENCTYPE='multipart/form-data'
method='POST' action='/myservlet'>
<INPUT TYPE='file' NAME='mptest'>
<INPUT TYPE='submit' VALUE='upload'>
</FORM>
The input type &quot;file&quot; brings up a button for a file select box on the browser together with a text field that takes the file name once selected. The servlet can use the GET method parameters to decide what to do with the upload while the POST body of the request contains the file data to parse.
When the user clicks the "Upload" button, the client browser locates the local file and sends it using HTTP POST, encoded using the MIME-type multipart/form-data. When it reaches your servlet, your servlet must process the POST data in order to extract the encoded file. You can learn all about this format in RFC 1867.
Unfortunately, there is no method in the Servlet API to do this. Fortunately, there are a number of libraries available that do. Some of these assume that you will be writing the file to disk; others return the data as an InputStream.
Once you process the form-data stream into the uploaded file, you can then either write it to disk, write it to a database, or process it as an InputStream, depending on your needs. See How can I access or create a file or folder in the current directory from inside a servlet? and other questions in the Servlets:Files Topic for information on writing files from a Servlet.
Please note that you can't access a file on the client system directly from a servlet; that would be a huge security hole. You have to ask the user for permission, and currently form-based upload is the only way to do that.
[This FAQ based on earlier posts by Thomas Moore, Detlef Pleiss (dpleiss@os-net.de), and others.]
分享到:
相关推荐
计算机网络安全中文件上传漏洞是一种常见的Web安全漏洞,攻击者可以通过上传恶意代码的文件来获取服务器的控制权。为了防御这种攻击,需要对文件上传进行严格的校验检测,限制上传文件的大小,检测文件名、后缀名和...
Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-文件上传Socket网络编程-...
C++实现多线程网络文件传输、
利用java图形化界面和网络编程相结合实现的--文件上传。 运行步骤: (1)分别运行工程两个包中的两个.java文件(UploadClient.java和UploadServer.java)分别会弹出“上传客服端”和“上传服务器”两个窗口。 ...
基于TCP的Socket网络编程之文件上传下载
在网络上找到的,纯JSP实现的文件上传程序,支持多文件的上传,例子是多文件的上传,稍微修改就可以变成单文件的上传或者更多文件的上传,控制成需要扩展名的文件上传,指定大小的文件上传等。程序目前上传文件存储...
Android之网络文件上传下载源码 在 Android 开发中,文件上传和下载是非常常见的功能,本文将从源码角度介绍如何实现 Android 之网络文件上传下载。 一、Layout 文件 Main.xml 在 Main.xml 文件中,我们可以看到...
C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#写的网络文件传送工具C#...
android从网络上下载文件
利用java图形化界面和网络编程相结合实现的--文件上传。 运行步骤: (1)分别运行工程两个包中的两个.java文件(UploadClient.java和UploadServer.java)分别会弹出“上传客服端”和“上传服务器”两个窗口。 ...
1. 简单快捷,只需要一句代码即可请求网络和文件处理 2. 只需要设置一个参数就能在界面上显示一个loading状态 3. block回调代码可读性更高 4. 快速上传下载文件,同时显示下载或上传进度条 5. 服务器请求接口单独用...
Java网络编程之TCP协议下—上传文件到服务器程序,欢迎大家下载和知道
4gl代码:实现ERP系统上传文件和下载文件,存在服务器上但不写人数据库
上传数据时会根据网络状况来控制数据包大小, 避免网络堵塞 7、服务端支持Windows 2003 Server/Windows 2000 Server/Windows NT等操作系统 8、文件经过微软代码签名认证,安装后可以在IE内正常加载。如果您使用我们的...
使用base64进行文件上传的具体流程是:前台使用js将文件转换为base64格式,后台通过高级编程语言,将base64格式的文件,转换为原文件。下面就来演示一下,C#语言配合js,如何实现图片的base64格式上传与解析保存。...
多种文件读取路径: 网络共享(UNC),本地文件,http(web共享)
java开发的网络文件传输,包括了源代码和课程设计报告。
该方法通过将大文件分块,并使用多线程技术来实现文件传输,提高了文件传输的效率。 知识点1:多线程编程 * 多线程编程是指在一个进程中同时执行多个线程,以提高程序的执行效率。 * 在Linux系统下,多线程编程...
VB编程,实现上传本地文件到服务器
题目如下: 编写程序实现文件上传功能。 有能力的同学可以自行设计文件下载功能,但是本次...要求支持多客户端并发上传文件,不考虑文件名冲突,假设多个客户端同时上传的文件没有重名现象。 必须使用多线程编程。