发送二进制数据到一个servlet [英] Sending binary data to a servlet
问题描述
函数sendToServlet(){
var file = Components.classes [@ mozilla.org/file/local;1]。
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(C:\\Documents and Settings\\\\\\\\\\ Documentus \\
var boundary =--------------+(new Date).getTime();
var stream = Components.classes [@ mozilla.org/network/file-input-stream; 1]
.createInstance(Components.interfaces.nsIFileInputStream);
stream.init(文件,0x04 | 0x08,0644,0x04); //文件是一个nsIFile实例
//发送
var req = Components.classes [@ mozilla.org/xmlextras/xmlhttprequest; 1]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
req.open('POST','http:// localhost:8080 / app / server',false);
var contentType =multipart / form-data; boundary =+ boundary;
req.setRequestHeader(Content-Type,contentType);
req.send(stream);
$ b
javascript的源代码:
https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Sending_binary_data
但不起作用。
这是使用的serlevt代码:
protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
// TODO自动生成的方法存根
int size = 1024 * 20000;
long sizeFile = 0;
File savedFile = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(new Long( - 1));
List items = null;
尝试{
items = upload.parseRequest(request);
} catch(FileUploadException e){
e.printStackTrace();
}
Iterator itr = items.iterator(); ($)
while(itr.hasNext()){
FileItem item =(FileItem)itr.next();
尝试{
if(item.isFormField()){
;
} else {
String itemName = item.getName();
int sizeName = itemName.length();
int end = itemName.indexOf('\\\
');
int start = itemName.lastIndexOf('\\\');
itemName = itemName.substring(start + 1,sizeName-end-1);
savedFile = new File(C:\\ Documents and Settings\\eric.silva\\Meus documentos\\+ itemName);
item.write(savedFile);
}
} catch(Exception e){
e.printStackTrace();
$ b $ metodo
<但是,当我尝试发送文件的servlet不创建文件发送。
通过JavaScript的一个必要的环境来回收。 Mas o arquivonãoécriado no lado do servidor。 Acredito que ocódigoapresentado no site da MDN esteja incompleto。
当我尝试通过javascript发送请求时发送。但该文件不是在服务器端创建的。我相信在MDN网站上显示的代码是不完整的。
解决方案 注意你使用的示例代码是如何发送数据使用方法 PUT
- 有效 multipart-formdata
请求需要有一些额外的头文件,而不仅仅是文件本身。例如,您发送的文件应该有一个名称(通常是表单字段的名称)。您应该使用 FormData
对象而不是,它会自动生成一个有效的请求。您应该能够创建一个文件
对象。
var file = File(C:\\Documents and Settings\\me\\ \\\ Document document \\ \\\\\\\\\\\\\\\\\\'');
var data = new FormData();
data.append(file,file);
var req = Components.classes [@ mozilla.org/xmlextras/xmlhttprequest; 1]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
req.open('POST','http:// localhost:8080 / app / server',false);
request.send(data);
请注意,像这样创建 File
只支持从Firefox 6开始。
I am trying send a file to a servlet.
function sendToServlet(){
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\Documents and Settings\\me\\Meus documentos\\Downloads\\music.mp3");
var boundary = "--------------" + (new Date).getTime();
var stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
stream.init(file, 0x04 | 0x08, 0644, 0x04); // file is an nsIFile instance
// Send
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
req.open('POST', 'http://localhost:8080/app/server' , false);
var contentType = "multipart/form-data; boundary=" + boundary;
req.setRequestHeader("Content-Type", contentType);
req.send(stream);
}
The source of javascript: https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Sending_binary_data
But does not work.
Hi, this the serlevt code used:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int size = 1024*20000;
long sizeFile = 0;
File savedFile = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(new Long("-1"));
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
try {
if (item.isFormField()) {
;
}else{
String itemName = item.getName();
int sizeName = itemName.length();
int end = itemName.indexOf('\n');
int start = itemName.lastIndexOf('\\');
itemName = itemName.substring(start + 1, sizeName-end-1);
savedFile = new File("C:\\Documents and Settings\\eric.silva\\Meus documentos\\"+itemName);
item.write(savedFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}//metodo
But when i try to send a file the servlet dont create the file sent. Quando eu tento enviar via javascript a requisição é enviada. Mas o arquivo não é criado no lado do servidor. Acredito que o código apresentado no site da MDN esteja incompleto.
When I try to send via javascript the request is sent. But the file is not created on the server side. I believe the code shown on the site of the MDN is incomplete.
Note how the example code you are using is sending data with method PUT
- valid multipart-formdata
request needs to have some additional headers, not only the file itself. For example, the file you are sending should have a name (normally the name of the form field). You should use a FormData
object instead, it will generate a valid request automatically. You should be able to create a File
object directly. Something along these lines:
var file = File("C:\\Documents and Settings\\me\\Meus documentos\\Downloads\\music.mp3");
var data = new FormData();
data.append("file", file);
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
req.open('POST', 'http://localhost:8080/app/server', false);
request.send(data);
Note that creating File
objects like this is only supported starting with Firefox 6.
这篇关于发送二进制数据到一个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!