简体   繁体   English

处理多部分/表单数据

[英]processing multipart/form-data

I'm doing file upload using XMLHttpRequest() in my jsp and when I do request.getContentType() in my controller I'm getting: 我正在使用jsp中的XMLHttpRequest()进行文件上传,而当我在控制器中执行request.getContentType()时,我会得到:

multipart/form-data; boundary=---------------------------4664151417711.

Further I'm not getting how to get the file and get the contents of it in my controller. 此外,我没有得到如何获取文件以及如何在控制器中获取其内容的信息。 Please anyone help. 请任何人帮助。

Update -- I'm doing this in my jsp. 更新-我正在我的jsp中执行此操作。

 function fileUpload() {
var url= document.getElementById("urlId").value;
 var file= document.getElementById("xslId").files[0];
 var formdata = new FormData();
 formdata.append("url", url);
 formdata.append("file", file);
 var xhr = new XMLHttpRequest();       
 xhr.open("POST","http://localhost:8080/XlsUpload/openSource.htm", true);
 xhr.send(formdata);
 xhr.onload = function(e) {
    };                    
   }   

and in my controller-- 在我的控制器中

 public void openSource(@ModelAttribute("domTool") DomTool   domTool,HttpServletRequest     request,HttpServletResponse response){
     String type=request.getContentType();

Further I'm struck how to get the contents of the uploaded file and the value of text field ie,URL in my controller. 此外,我对如何获取上载文件的内容以及控制器中文本字段(即URL)的值感到震惊。 The type i'm getting as multipart/form-data 我要作为多部分/表单数据的类型

There is an Apache commons solution called commons-fileupload for parsing multipart content. 有一个Apache commons解决方案,称为commons-fileupload用于解析多部分内容。 You can find it here . 你可以在这里找到它。

The most simple example copied from their tutorial looks like this: 从他们的教程中复制的最简单的示例如下所示:

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = upload.parseRequest(request);

    // iterate over items (i.e. list of FileItem) and access 
    // the content with getInputStream()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 multipart / form-data的问题 - Issue with multipart/form-data 多部分/表单数据表单提交 - Multipart/form-data form submit 处理 multipart/form-data 请求时,srpingboot 1.3.7 中的文件始终为 null - File is always null in srpingboot 1.3.7 when processing multipart/form-data request 它不起作用! 处理多部分/表单数据请求失败。 套接字上意外的EOF读取 - Its not working!! Processing of multipart/form-data request failed. Unexpected EOF read on the socket 处理multipart / form-data请求失败。读取超时 - Processing of multipart/form-data request failed. Read timed out org.apache.commons.fileupload.FileUploadException:处理多部分/表单数据请求失败 - org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed 无法解析多部分请求org.apache.commons.fileupload.FileUploadException:处理多部分/表单数据请求失败。 空值 - Failed to parse multipart request org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed. null WebSocket多部分/表单数据仿真可以吗? - WebSocket multipart/form-data emulation possible? 使用 Undertow 的多部分表单数据示例 - Multipart form-data example using Undertow 无法发送多部分/表单数据 - impossible to send multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM