javax.servlet.http.Part 到 java.io.File
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30810014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.servlet.http.Part to java.io.File
提问by Peter Penzov
I need help in code which is used to convert javax.servlet.http.Part to java.io.File
我需要用于将 javax.servlet.http.Part 转换为 java.io.File 的代码方面的帮助
I found this useful code but I need help in properly implementing the code.
我找到了这个有用的代码,但我需要帮助才能正确实现代码。
private void processFilePart(Part part, String filename) throws IOException
{
filename = filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1);
String prefix = filename;
String suffix = "";
if (filename.contains("."))
{
prefix = filename.substring(0, filename.lastIndexOf('.'));
suffix = filename.substring(filename.lastIndexOf('.'));
}
File file = File.createTempFile(prefix + "_", suffix, new File(location));
if (multipartConfigured)
{
part.write(file.getName());
}
else
{
InputStream input = null;
OutputStream output = null;
try
{
input = new BufferedInputStream(part.getInputStream(), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(file), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
for (int length = 0; ((length = input.read(buffer)) > 0);)
{
output.write(buffer, 0, length);
}
}
finally
{
if (output != null)
try
{
output.close();
}
catch (IOException logOrIgnore)
{
}
if (input != null)
try
{
input.close();
}
catch (IOException logOrIgnore)
{
}
}
}
put(part.getName(), file);
part.delete();
}
I tried to edit the code in order to create as a result java.io.File but I always have issues.
我试图编辑代码以创建结果 java.io.File 但我总是有问题。
private void processFilePart(Part part, String filename) throws IOException
{
int DEFAULT_BUFFER_SIZE = 2048;
filename = filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1);
String prefix = filename;
String suffix = "";
if (filename.contains("."))
{
prefix = filename.substring(0, filename.lastIndexOf('.'));
suffix = filename.substring(filename.lastIndexOf('.'));
}
File file = new File(filename);
InputStream input = null;
OutputStream output = null;
try
{
input = new BufferedInputStream(part.getInputStream(), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(file), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
for (int length = 0; ((length = input.read(buffer)) > 0);)
{
output.write(buffer, 0, length);
}
}
finally
{
if (output != null)
try
{
output.close();
}
catch (IOException logOrIgnore)
{
}
if (input != null)
try
{
input.close();
}
catch (IOException logOrIgnore)
{
}
}
// how to get the result
part.delete();
}
What is the proper way to convert the objects?
转换对象的正确方法是什么?
回答by Mauricio Gracia Gutierrez
The fileupload Example Application - Taken from http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
文件上传示例应用程序 - 取自http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
The fileupload example illustrates how to implement and use the file upload feature.
fileupload 示例说明了如何实现和使用文件上传功能。
The fileupload example application consists of a single servlet and an HTML form that makes a file upload request to the servlet.
文件上传示例应用程序由一个 servlet 和一个 HTML 表单组成,该表单向 servlet 发出文件上传请求。
This example includes a very simple HTML form with two fields, File and Destination. The input type, file, enables a user to browse the local file system to select the file. When the file is selected, it is sent to the server as a part of a POST request. During this process two mandatory restrictions are applied to the form with input type file:
这个例子包括一个非常简单的 HTML 表单,它有两个字段,文件和目的地。输入类型文件使用户能够浏览本地文件系统以选择文件。选择文件后,它会作为 POST 请求的一部分发送到服务器。在此过程中,两个强制限制应用于具有输入类型文件的表单:
The enctype attribute must be set to a value of multipart/form-data.
Its method must be POST.
enctype 属性必须设置为 multipart/form-data 的值。
它的方法必须是POST。
When the form is specified in this manner, the entire request is sent to the server in encoded form. The servlet then handles the request to process the incoming file data and to extract a file from the stream. The destination is the path to the location where the file will be saved on your computer. Pressing the Upload button at the bottom of the form posts the data to the servlet, which saves the file in the specified destination.
当以这种方式指定表单时,整个请求将以编码形式发送到服务器。然后 servlet 处理请求以处理传入的文件数据并从流中提取文件。目标是文件将在您的计算机上保存的位置的路径。按表单底部的上传按钮将数据发布到 servlet,servlet 将文件保存在指定的目的地。
The HTML form in tut-install/examples/web/fileupload/web/index.html is as follows:
tut-install/examples/web/fileupload/web/index.html中的HTML表单如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="upload" enctype="multipart/form-data" >
File:
<input type="file" name="file" id="file" /> <br/>
Destination:
<input type="text" value="/tmp" name="destination"/>
</br>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>
</html>
A POST request method is used when the client needs to send data to the server as part of the request, such as when uploading a file or submitting a completed form. In contrast, a GET request method sends a URL and headers only to the server, whereas POST requests also include a message body. This allows arbitrary-length data of any type to be sent to the server. A header field in the POST request usually indicates the message body's Internet media type.
当客户端需要将数据作为请求的一部分发送到服务器时,使用 POST 请求方法,例如上传文件或提交完整的表单时。相比之下,GET 请求方法仅向服务器发送 URL 和标头,而 POST 请求还包括消息正文。这允许将任意类型的任意长度数据发送到服务器。POST 请求中的标头字段通常指示消息正文的 Internet 媒体类型。
When submitting a form, the browser streams the content in, combining all parts, with each part representing a field of a form. Parts are named after the input elements and are separated from each other with string delimiters named boundary.
提交表单时,浏览器将内容流式传输,合并所有部分,每个部分代表表单的一个字段。部分以输入元素命名,并使用名为边界的字符串分隔符相互分隔。
This is what submitted data from the fileupload form looks like, after selecting sample.txt as the file that will be uploaded to the tmp directory on the local file system:
这是从 fileupload 表单提交的数据的样子,在选择 sample.txt 作为将上传到本地文件系统上的 tmp 目录的文件后:
POST /fileupload/upload HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data;
boundary=---------------------------263081694432439
Content-Length: 441
-----------------------------263081694432439
Content-Disposition: form-data; name="file"; filename="sample.txt"
Content-Type: text/plain
Data from sample file
-----------------------------263081694432439
Content-Disposition: form-data; name="destination"
/tmp
-----------------------------263081694432439
Content-Disposition: form-data; name="upload"
Upload
-----------------------------263081694432439--
The servlet FileUploadServlet.java can be found in the tut-install/examples/web/fileupload/src/java/fileupload/ directory. The servlet begins as follows:
servlet FileUploadServlet.java 可以在 tut-install/examples/web/fileupload/src/java/fileupload/ 目录中找到。servlet 开始如下:
@WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload"})
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
private final static Logger LOGGER =
Logger.getLogger(FileUploadServlet.class.getCanonicalName());
The @WebServlet annotation uses the urlPatterns property to define servlet mappings.
@WebServlet 注释使用 urlPatterns 属性来定义 servlet 映射。
The @MultipartConfig annotation indicates that the servlet expects requests to made using the multipart/form-data MIME type.
@MultipartConfig 注释表明 servlet 期望使用 multipart/form-data MIME 类型发出请求。
The processRequest method retrieves the destination and file part from the request, then calls the getFileName method to retrieve the file name from the file part. The method then creates a FileOutputStream and copies the file to the specified destination. The error-handling section of the method catches and handles some of the most common reasons why a file would not be found. The processRequest and getFileName methods look like this:
processRequest 方法从请求中检索目标和文件部分,然后调用 getFileName 方法从文件部分检索文件名。然后该方法创建一个 FileOutputStream 并将文件复制到指定的目的地。该方法的错误处理部分会捕获并处理一些无法找到文件的最常见原因。processRequest 和 getFileName 方法如下所示:
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// Create path components to save the file
final String path = request.getParameter("destination");
final Part filePart = request.getPart("file");
final String fileName = getFileName(filePart);
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();
try {
out = new FileOutputStream(new File(path + File.separator
+ fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + path);
LOGGER.log(Level.INFO, "File{0}being uploaded to {1}",
new Object[]{fileName, path});
} catch (FileNotFoundException fne) {
writer.println("You either did not specify a file to upload or are "
+ "trying to upload a file to a protected or nonexistent "
+ "location.");
writer.println("<br/> ERROR: " + fne.getMessage());
LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}",
new Object[]{fne.getMessage()});
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
}
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}