- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Internet Explorer 7、8 和 9 中上传图片时遇到问题(尚未测试其他版本)。它在 FF 和 Chrome 中工作正常,但由于某种原因,当我尝试在任何版本的 IE 中上传图片时,图片上传错误。
我的意思是,文件以正确的名称上传到正确的目录中,但我无法在任何图片编辑程序中打开它。
此外,当我在 Notepad++ 中打开图片时,我看到图片显示了一些元数据,如下所示:
----------------------------7db1f6c907fe内容处置:表单数据;名称=“QQ文件”;文件名=“jingjang.jpg”内容类型:图像/jpeg
(这里是哈希码)
----------------------------7db1f6c907fe--
如果我删除代码,图片就可以正常工作!那么谁能告诉我什么在生成代码以及如何停止它? :)
我在 JSP 页面上使用 Valums Ajax Upload:
var imageFolder = "images/upload/<%=user.getUsername()%>/temp/";
new qq.FileUploader({
element: document.getElementById('TempUploadButton'),
action: 'OctetStreamReader',
debug: false,
multiple: false,
params: {"type" : "user"},
onComplete: function(id, fileName) {
var d = new Date();
$("#UserPageAvatarPic a img").attr("src", imageFolder+"<%=user.getUsername()%>.jpg?cache="+d.getTime() );
},
onSubmit : function(id, fileName) {
// hide all prev elements
$('#TempUploadButton ul.qq-upload-list li').each(function() {
$(this).hide();
});
}
});
OctetStreamReader 作为我的 servlet
public class OctetStreamReader extends HttpServlet {
private static final long serialVersionUID = 6748857432950840322L;
private static final String DESTINATION_DIR_PATH = "files";
private static String realPath;
UserService userService = UserService.getService();
/**
* {@inheritDoc}
* @param config
* @throws ServletException
*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH) + "/";
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
User user = userService.getUser(((Integer) request.getSession().getAttribute("user")).intValue());
String type = request.getParameter("type");
String username = user.getUsername();
PrintWriter writer = null;
InputStream is = null;
FileOutputStream fos = null;
type = "user";
try {
writer = response.getWriter();
} catch (IOException ex) {
log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
}
try {
String filename = "";
if (type.equals("user")) {
realPath = getServletContext().getRealPath("/images/upload/" + username + "/temp/");
is = request.getInputStream();
String strDirectoy = getServletContext().getRealPath("/images/upload/" + username + "/temp/" );
boolean success = (new File(strDirectoy)).mkdir();
File f1 = new File(strDirectoy);
File[] children = f1.listFiles();
filename = username + ".jpg";
}
if (type.equals("post")) {
realPath = getServletContext().getRealPath("/images/upload/" + username + "/post/");
is = request.getInputStream();
String strDirectoy = getServletContext().getRealPath("/images/upload/" + username + "/post/" );
boolean success = (new File(strDirectoy)).mkdir();
File f1 = new File(strDirectoy);
File[] children = f1.listFiles();
filename = Calendar.getInstance().getTimeInMillis()+".jpg";
}
if (type.equals("editpost")) {
realPath = getServletContext().getRealPath("/images/upload/" + username + "/editpost/");
is = request.getInputStream();
String strDirectoy = getServletContext().getRealPath("/images/upload/" + username + "/editpost/" );
boolean success = (new File(strDirectoy)).mkdir();
File f1 = new File(strDirectoy);
File[] children = f1.listFiles();
filename = Calendar.getInstance().getTimeInMillis() + ".jpg";
}
fos = new FileOutputStream(new File(realPath + "/" + filename), false);
IOUtils.copy(is, fos);
response.setStatus(response.SC_OK);
writer.print("{success: true, filename: \"" + filename + "\"}");
} catch (FileNotFoundException ex) {
response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
writer.print("{success: false}");
log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
} catch (IOException ex) {
response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
writer.print("{success: false}");
log(OctetStreamReader.class.getName() + "has thrown an exception: " + ex.getMessage());
} finally {
try {
fos.close();
is.close();
} catch (IOException ignored) {
}
}
writer.flush();
writer.close();
}
}
另外,在 fileuploader.js 中,我尝试将内容类型从 application/octet-stream 更改为 multipart/form-data
/**
* Sends the file identified by id and additional query params to the server
* @param {Object} params name-value string pairs
*/
_upload: function(id, params){
var file = this._files[id],
name = this.getName(id),
size = this.getSize(id);
this._loaded[id] = 0;
var xhr = this._xhrs[id] = new XMLHttpRequest();
var self = this;
xhr.upload.onprogress = function(e){
if (e.lengthComputable){
self._loaded[id] = e.loaded;
self._options.onProgress(id, name, e.loaded, e.total);
}
};
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
self._onComplete(id, xhr);
}
};
// build query string
params = params || {};
params['qqfile'] = name;
var queryString = qq.obj2url(params, this._options.action);
xhr.open("POST", queryString, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(file);
},
最佳答案
HTML文件上传默认使用the multipart/form-data
request encoding (这是为了能够上传多个文件和/或在一个请求中混合正常的输入字段值)。然而,您并不是从请求正文中解析和提取各个表单数据部分,而是读取整个请求正文并将其写入未修改的文件中。
is = request.getInputStream();
fos = new FileOutputStream(new File(realPath + "/" + filename), false);
IOUtils.copy(is, fos);
这确实行不通。你说它可以在 FF/Chrome 中运行,这超出了我的范围。也许您误解了结果,或者根本没有在那些带有二进制文件的浏览器中对其进行测试。
您需要使用Apache Commons FileUpload从 multipart/form-data
请求中提取部分。或者,当您已经使用 Servlet 3.0 时,您也可以使用提供的 Servlet API HttpServletRequest#getParts()
方法。
与具体问题无关,到目前为止发布的代码还有另一个问题。您将上传的文件存储在扩展 WAR 的 Web 内容中。这距离坚固的永久存储位置还很远。每次重新部署新的 WAR 时,所有这些文件都会丢失。您每次都需要备份它们,这非常笨拙且容易出错。而是将它们存储在扩展的 WAR 文件夹之外的固定位置。
关于java - 使用值 ajax upload 和 OctetStreamReader 上传的图片在 IE 中不起作用 - Java/JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7796371/
我在项目中使用ngx-uploader实现文件上传。 但是当我上传多个文件时,它将文件数组分成多个请求。 我尝试使用 ng2-file-upload 但结果相同。 最佳答案 请参阅 GitHub 上的
我想要一个类似 this 的上传者但我想要一个进度条,并在完成后通过电子邮件向我发送通知,就像 yousendit 那样。 任何开源的东西都会很酷。 最佳答案 Uploadify允许有进度条。至于电子
我正在尝试编写一个Python脚本,可以将图片和pdf上传到WordPress。我希望图像上传到文件夹‘/wp-Content/Uploads/’,将pdf文件上传到文件夹‘/wp-Content/U
开发自定义 portlet 以在 Liferay 6.2 中上传多个文件。 在以下位置的文档库 Portlet 中浏览 Liferay 源代码时找到 Liferay.Upload 组件: https:
我正在尝试使用 HTML5 制作一个带有进度表的文件 uploader 。这是我的代码: Test Progress Meter function submit
当我选择一些图像并放入 WordPress 文件 uploader 时,该组的第一张图像此时似乎已正确上传,而其他图像则卡住且未得到处理。 但是,经过一段时间的等待,我停止了该进程,重新加载了浏览器选
我今天刚刚从 Cordova (PhoneGap) 1.5 升级到 1.9,突然我的 FileTransfer 参数停止发布。我可以说出来,因为我让服务器端调试了 $_POST 参数,它们现在是空白的
我已经在运行 RHEL7 的服务器上安装了 Mediawiki v1.24.1。 我已经将它安装在/var/www/foohelp/wiki 下。但是,当我尝试上传文件时,出现以下错误: [f3eae
在 Symfony2 中上传图片时,有没有办法调整图片大小? ImagineAvalancheBundle只允许在检索图像时将图像大小调整为缩略图,这对我来说并不是真正的性能。 此外,在发布数据时检索
我在网站上使用blueimp-file-upload,并且在使用webpack来组织我的js代码。 我从NPM安装了blueimp-file-upload和jquery.ui.widget npm i
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我需要获取上传的文件以将其推送到文件列表,但我无法做到这一点...我希望有人可以帮助我: UIkit.upload('.test-upload', { url: `/api/gridfs/${d
我基本上是一名 Java 开发人员,仅了解有关 Android 开发的基本信息。我开发了一个 Web 端点,它接受文件和一些其他参数。 java代码是 @RequestMapping(path = "
我正在使用 symfony.com 的食谱文章来实现图像的文件上传选项。 现在我想将其他图像加载到实体中。 默认的编辑策略是: 1.从数据库中取出 2. 注入(inject)表单 3.坚持 不知何故,
我需要处理通过(有和没有分块)上传到 Amazon S3 的每个文件的二进制数据。你知道 Fineuploader 中是否有我可以用来处理每个二进制 block /文件的函数/信号吗?: 例如: pr
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我读到 HTML5 规范引入了在上传表单中选择多个文件的功能。目前有哪些浏览器支持这个? Adobe AIR 是否支持它? 额外的问题:是否有利用此功能的 JavaScript 库? 最佳答案 即将发
我正在评估 Fine Uploader与其他各种选项相比,特别是 JQuery File Upload . 与依赖 Bootstrap 和 JQuery UI 的 JQuery File Upload
我正在尝试通过 Swift 2/Alamofire 将文件和参数上传到 Google 云端硬盘。在下面的代码中,我更改了以下行: "https://www.googleapis.com/upload/
我正在使用 Kendo UI Upload Control 并希望在同步模式下允许多个文件,但是当同时添加多个文件时,它们被组合在同一行项目中。有没有办法在组选择时将每个单独的文件作为自己的行项目?在
我是一名优秀的程序员,十分优秀!