我正在尝试实现dojox.form.Uploader以上传文件。我在JAVA中创建了一个简单的REST服务。 从Firefox和Chrome,该服务获取表单并保存数据,但IE8并非如此。
在IE8中,与在FF / Chr中触发的对象相比,我获得了完全不同的响应对象。它实际上是一个包含文件信息的数组,都带有'错误' - '服务器超时'消息。事实上,表单提交甚至没有达到服务。
我刚开始使用JAVA REST服务,所以请原谅任何明显的错误。
谢谢堆。 微米。
客户端代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Uploading test</title>
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/resources/dojo.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/tundra/tundra.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/nihilo/nihilo.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/soria/soria.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/FileUploader.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/UploaderFileList.css" />
<link rel="stylesheet" type="text/css"
href="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/form/resources/FileInput.css" />
<script type="text/javascript">djConfig = { parseOnLoad:true, isDebug:true, dojoBlankHtmlUrl: 'blank.html' };</script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"></script>
<script>
dojo.require("dojox.form.Uploader");
dojo.require("dojox.form.uploader.FileList");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");
dojo.require("dojo.io.iframe");
dojo.require("dojox.form.FileInput");
dojo.require("dojox.form.uploader.plugins.Flash");
dojo.require("dojox.form.uploader.plugins.HTML5");
function prepareForm(){
var form = dojo.byId("myform");
// Disable the button at startup
//dijit.byId('submitId').set("disabled", true);
// Connect to the onChange event of file upload stuff.
dojo.connect(dojo.byId("uploader"), "onchange", function(){
//checkExtension();
});
// Connect to the onChange event of file upload stuff.
dojo.connect(dijit.byId("uploader"), "onComplete", function(response){
dojo.byId("response").innerHTML = "Form posted with status : " + response;
});
}
dojo.ready(prepareForm);
</script>
</head>
<body class="soria">
<b>Simple Form:</b>
<br>
<form method="post" action="jersey/fileupload2" id="myForm"
enctype="multipart/form-data">
<legend>file upload test</legend>
<input name="uploadedfile" multiple="true" type="file" id="uploader"
dojoType="dojox.form.Uploader" label="Select XLSX Files"
style="width: 150px;">
<div id="files" dojoType="dojox.form.uploader.FileList"
uploaderId="uploader" style="width: 300px;"></div>
<br /> <input type="submit" label="Submit"
dojoType="dijit.form.Button" id="submitId" />
</form>
<br>
<b>Result</b>
<div id="response"></div>
</body>
</html>
服务器端代码
@Path("fileupload2")public class FileUploadResource2 {
@POST
@Consumes("multipart/form-data")
@Produces("text/html")
public String loadFile(@Context HttpServletRequest request) {
String resultStatus="{response:'fail'}";
String fileRepository="C:\\TEMP\\";
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
if(items!=null) {
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if(!item.isFormField() && item.getSize() > 0) {
String fileName = processFileName(item.getName());
resultStatus="{response:'ok.'}";
try {
//throw new Exception("error happened.");
item.write(new File(fileRepository+fileName));
} catch (Exception e) {
resultStatus="{response:'failed.'}";
//e.printStackTrace();
//return "{result:'" + e.fillInStackTrace() + "'}";
}
}
}
}
}
return resultStatus;
}
private String processFileName(String fileNameInput) {
String fileNameOutput=null;
fileNameOutput = fileNameInput.substring(fileNameInput.lastIndexOf("\\")+1,fileNameInput.length());
return fileNameOutput;
}
}
答案 0 :(得分:0)
这是我的解决方案:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String dir = getServletConfig().getInitParameter(
"javax.servlet.context.tempdir");
logger.info("FileUpload: doPost called, directory for the upload: "
+ dir);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
logger.info("Is multipart: " + isMultipart);
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
if (isMultipart) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
ArrayList<FileInfo> fileInfos = new ArrayList<FileInfo>();
boolean html5 = false;
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
logger.info("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
logger.info("File field " + name + " with file name "
+ item.getName() + " detected.");
if (name.contains("uploadedfiles"))
html5 = true;
// Process the input stream
File file = new File(dir + item.getName());
FileUtils.copyInputStreamToFile(stream, file);
FileInfo e = new FileInfo();
e.setName(item.getName());
e.setFile(item.getName());
e.setExt(item.getContentType());
fileInfos.add(e);
}
}
printJSON(out, html5, fileInfos);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void printJSON(ServletOutputStream out, boolean html5, ArrayList<FileInfo> fileInfos)
throws IOException {
if (!html5)
out.print("<textarea>");
out.print("[");
for (FileInfo fileInfo : fileInfos) {
out.print("{");
out.print("\"name\": \"" + fileInfo.getName() + "\",");
out.print("\"file\": \"" + fileInfo.getFile() + "\",");
out.print("\"type\": \"" + fileInfo.getExt() + "\"");
out.print("}");
}
out.print("]");
if (!html5)
out.print("</textarea>");
}
FileInfo是一个包含以下字段的简单类:
private String name;
private String file;
private String ext;