- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Android 中上传图像时遇到问题。
我正在使用 apache httpmime 4.1 lib代码是这样的:
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", new FileBody(new File(AndorraApplication.getPhotosPath() + "/" + entity.getFileName()), "image/jpeg"));
resp = NetworkUtils.sendHttpRequestMultipart(EXPORT_PHOTOS_URI, reqEntity);
NetworkUtils 类:
public class NetworkUtils {
public static final int REGISTRATION_TIMEOUT = 3 * 1000;
public static final int WAIT_TIMEOUT = 5 * 1000;
public static HttpResponse sendHttpRequestMultipart(String uri, MultipartEntity entity) {
HttpClient mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
HttpPost post = new HttpPost(uri);
post.addHeader(entity.getContentType());
post.setEntity(entity);
HttpResponse resp = mHttpClient.execute(post);
}
}
有时一切正常,但有时(特别是在连接速度较慢时)图像上传时非常损坏。示例在这里:http://pixelbirthcloud.com/574_orig.jpg
它不会抛出任何异常。上传文件的长度与原始文件的长度相同..尝试将 mime 类型更改为 application/octet-stream 或完全删除它。尝试使用超时。还是一样的结果。最终用户几乎总是上传损坏的图像(尽管我只成功获得了 bronem 图像 2 次).. 图像大小最初为 2.5 兆,但后来我将其减小到 500-700 kb。但没有解决问题。
还没有尝试更改 apache 的库..也许这就是问题所在..但据我阅读网络,没有人在使用 httpmime 库时遇到过这种情况。
它可能是什么?我现在完全迷失了:(
另一个问题是超时有时不起作用。
就像这行代码一样: HttpResponse resp = mHttpClient.execute(post);我禁用 3g 连接,它只会等待 17-20 分钟,而不是 3 或 5 秒..然后才抛出异常。尝试了不同的方法。像这样:
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
ConnManagerParams.setMaxTotalConnections(params, 5);
ConnManagerParams.setTimeout(params, 30000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https",PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
HttpClient httpclient = new DefaultHttpClient(manager, params);
但还是不行:)
最佳答案
查看我的图像 uploader 代码,它对我来说非常有用此类将文件上传到服务器,最后还读取 XML 回复。根据您的要求过滤代码。它对我来说非常顺利
package com.classifieds;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class Uploader
{
private String Tag = "UPLOADER";
private String urlString ;//= "YOUR_ONLINE_PHP";
HttpURLConnection conn;
String exsistingFileName ;
private void uploadImageData(String existingFileName , String urlString)
{
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
// ------------------ CLIENT REQUEST
Log.e(Tag, "Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(
exsistingFileName));
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos
.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
+ exsistingFileName + "" + lineEnd);
dos.writeBytes(lineEnd);
Log.v(Tag, "Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1000;
// int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bytesAvailable];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
while (bytesRead > 0) {
dos.write(buffer, 0, bytesAvailable);
bytesAvailable = fileInputStream.available();
bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.v(Tag, "File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.e(Tag, "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e(Tag, "error: " + ioe.getMessage(), ioe);
}
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = null;
try {
sp = spf.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the XMLReader of the SAXParser we created.
XMLReader xr = null;
try {
xr = sp.getXMLReader();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create a new ContentHandler and apply it to the XML-Reader
MyExampleHandler1 myExampleHandler = new MyExampleHandler1();
xr.setContentHandler(myExampleHandler);
// Parse the xml-data from our URL.
try {
xr.parse(new InputSource(conn.getInputStream()));
//xr.parse(new InputSource(new java.io.FileInputStream(new java.io.File("login.xml"))));
} catch (MalformedURLException e) {
Log.d("Net Disconnected", "NetDisconeeted");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Log.d("Net Disconnected", "NetDisconeeted");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
Log.d("Net Disconnected", "NetDisconeeted");
// TODO Auto-generated catch block
e.printStackTrace();
}
// Parsing has finished.
}
public Uploader(String existingFileName, boolean isImageUploading , String urlString ) {
this.exsistingFileName = existingFileName;
this.urlString = urlString;
}
class MyExampleHandler1 extends DefaultHandler
{
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
}
}
}
关于Android httpclient文件上传数据损坏和超时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5039685/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我目前正在尝试制作一个非常简单的应用程序,它会根据一天中的时间问候。我的代码是: open System let read() = Console.Read() let readLine() = Co
我已经运行Elasticsearch服务很长时间了,但是突然遇到了以下情况 由以下原因导致:org.elasticsearch.index.translog.TranslogCorruptedExce
我对执行以下操作的 php 重定向脚本有一个奇怪的问题: 在用户的浏览器中植入 Cookie,或者读取现有 Cookie(如果有)。 将用户重定向到另一个网址(重定向的网址是原始网址中的参数,例如 h
我正在使用 iText 7.0.0(Java 风格),似乎表格单元格 HorizontalAlignment 被忽略,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗? see th
简而言之: 我有一个可以从多个线程访问的计数器变量。尽管我已经实现了多线程读/写保护,但该变量似乎仍然以不一致的方式同时写入,导致计数器结果不正确。 深入杂草: 我使用的“for 循环”会在后台触发大
我有一个 REST 项目,在访问控制服务类中保存用户的ArrayList。一切都工作正常,直到 REST Web 服务突然抛出 java.util.NoSuchElementException。单步查
已关闭。此问题不符合Stack Overflow guidelines 。它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
当我刷新页面时,我无法显示 voteUp/Down,因为如果我执行 voteUp/Down(+1 或 -1) 并刷新页面,这会再次返回 voteUp/Down (0)。过去我使用 JSON,但社区推荐
我正在为离散时间 CPU 调度模拟器编写代码。它只是生成流程并相应地安排它们。我目前正在实现 FCFS 计划。我理解离散时间模拟器的本质,但我在用 C++ 实现时遇到了麻烦。 问题出现在handleN
尝试使用 yum 部署包时出现错误: 2016-07-07 14:14:31,296 - ERROR - error: rpmdb: BDB0113 Thread/process 6723/1
我有一个简单的同步队列 template class SynchronisedQueue { public: void Enqueue(const T& d
我正在使用 hadoop 0.20.append 和 hbase 0.90.0。我将少量数据上传到 Hbase,然后出于评估目的杀死了 HMaster 和 Namenode。在此之后,我向 Hbase
我使用 symfony 框架 1.4 创建了一个网站。我正在使用 sfguard 进行身份验证。 现在,这在 WAMP (windows) 上运行良好。我可以在不同的浏览器上登录多个帐户并使用该网站。
目前我已经实现了 HashMap private static Map cached = new HashMap(); 和 Item 是一个具有属性的对象 Date expireTime 和 byte
我试图将 2 个不同的 WPF 控件绑定(bind)到 ViewModel 中的同一属性,即 CheckBox.IsChecked 和 Expander.IsExpanded。我想要实现的行为是让 C
我希望这是一个简单的问题,但我没有找到答案。 我想让 build.gradle 文件通过替换某些变量来设置我的 Spring Boot 应用程序中的版本。这与广告一样有效: def tokens =
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
这个问题在这里已经有了答案: In a fragment shader, why can't I use a flat input integer to index a uniform array o
我已经下载了 OSM 世界地图。解析时出现异常: osm bound changeset (...) changeset Exception in thread "main" org.xml.sax.
我是一名优秀的程序员,十分优秀!