使用Java处理HTML(多部分表单数据)文件上传 [英] Handling HTML (multipart form-data) file uploads with Java

查看:114
本文介绍了使用Java处理HTML(多部分表单数据)文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为概念证明Web应用程序开发多文件上传解决方案。我正在使用java servlet来处理AJAX文件上传。我的问题是,java如何处理从HTML表单上传文件?如果有人可以解释如何处理一个基本的HTML文件上传,那么我可以将其转到我的解决方案。



快速切线:
我是一名Web开发人员,拥有C#& PHP。我试图跳上Java的潮流,现在我已经采取了一种新的立场,我的公司认为Java是所有编程语言的圣杯。我觉得我在这里错过了一些东西......我非常喜欢Java语言的感觉以及运行应用程序的容易程度。但它作为一种网络编程语言似乎无限困难。



预先致谢

解决方案

您可以使用 Commons FileUpload 库:

http://commons.apache.org/fileupload/

下面是它的用法的简单例子:

  //为基于磁盘的文件项目创建一个工厂
FileItemFactory factory = new DiskFileItemFactory();

//创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);

//解析请求
List / * FileItem * / items = upload.parseRequest(request);

我从这里得到这个例子:

http://commons.apache.org/fileupload/using.html


I'm working on a multiple file upload solution for a proof-of-concept web application. I'm using a java servlet to handle an AJAX file upload. My question is how does java handle uploading files from an HTML form? If someone could explain how a basic HTML file upload is processed then I could probably port this to my solution.

Quick tangent: I'm a web developer with a background in C# & PHP. I'm trying to hop on the Java bandwagon now that I've taken a new position where mycompany believes Java is the holy grail of all programming languages. I feel like I'm missing something here... I definitely like the feel of the Java language and how easy it is to run applications. But its seems infinitely difficult to use as a web programming language.

Thanks in advance.

解决方案

You can use Commons FileUpload library:

http://commons.apache.org/fileupload/

Here is simple example of it's usage:

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

I taken this example from here:

http://commons.apache.org/fileupload/using.html

这篇关于使用Java处理HTML(多部分表单数据)文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆