Activity no problem to get, can print out picPath
Upload can print out the path and file name
public class UploadUtil {
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; / / copy always Kun Kun jin jin beat
private static final String CHARSET = "utf-8"; / / Kun Kun Jiao Bie jin beat beat beat
Kun poundsprivate int readTimeOut = 10 * 1000; / / read timeout
private int connectTimeout = 10 * 1000; / / timeout
private static final String BOUNDARY = UUID.randomUUID () toString ();. / / boundary identification
/ / Randomly generated
private static final String PREFIX = "-";
private static final String LINE_END = "\ r \ n";
private static final String CONTENT_TYPE = "multipart / form-data"; / / content type
public int uploadFile (File file, String RequestURL, int id, String table) {
int res = 0;
Log.e ("file", file.getName () + "" + file.getPath ());
long requestTime = System.currentTimeMillis ();
long responseTime = 0;
String result = "";
String getUrl = RequestURL + "/ Upload.action";
try {
URL url = new URL (getUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setReadTimeout (readTimeOut);
conn.setConnectTimeout (connectTimeout);
conn.setDoInput (true); / / allow input stream
conn.setDoOutput (true); / / allows the output stream
conn.setUseCaches (false); / / do not allow the use of cache
conn.setRequestMethod ("POST"); / / request method
conn.setRequestProperty ("Charset", CHARSET); / / set the encoding
conn.setRequestProperty ("connection", "keep-alive");
conn.setRequestProperty ("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty ("Content-Type", CONTENT_TYPE + "; boundary ="
+ BOUNDARY);
DataOutputStream dos = new DataOutputStream (conn.getOutputStream ());
StringBuffer sb = null;
String params = "";
sb = null;
params = null;
sb = new StringBuffer ();
.. sb.append (PREFIX) append (BOUNDARY) append (LINE_END);
sb.append ("Content-Disposition: form-data; name = \" "+" pic "
+ "\"; Filename = \ "" + file.getName () + "\" "+ LINE_END);
sb.append ("Content-Type: image / pjpeg" + LINE_END);
sb.append (LINE_END);
params = sb.toString ();
sb = null;
Log.i (TAG, file.getName () + "=" + params + "# #");
dos.write (params.getBytes ());
InputStream is = new FileInputStream (file);
byte [] bytes = new byte [1024];
int len = 0;
int curLen = 0;
while ((len = is.read (bytes))! = -1) {
curLen + = len;
dos.write (bytes, 0, len);
}
is.close ();
dos.write (LINE_END.getBytes ());
byte [] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END)
. GetBytes ();
dos.write (end_data);
dos.flush ();
res = conn.getResponseCode ();
responseTime = System.currentTimeMillis ();
requestTime = (int) ((responseTime - requestTime) / 1000);
Log.e (TAG, "response code:" + res);
if (res == 200) {
Log.e (TAG, "request success");
InputStream input = conn.getInputStream ();
StringBuffer sb1 = new StringBuffer ();
int ss;
while ((ss = input.read ())! = -1) {
sb1.append ((char) ss);
}
result = sb1.toString ();
Log.e (TAG, "result:" + result);
res = uploadTable (result, id, table, RequestURL);
} Else {
Log.e (TAG, "request error");
}
} Catch (MalformedURLException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
}
return res;
}
public int doUpload (File file, String RequestURL, int id, String table) {
int res = 0;
String getUrl = RequestURL + "/ Upload.action";
String end = "\ r \ n";
String twoHyphens = "-";
String boundary = "*****";
Log.e ("file", file.getName () + "" + file.getPath ());
try {
URL url = new URL (getUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection ();
con.setDoInput (true);
con.setDoOutput (true);
con.setUseCaches (false);
con.setRequestMethod ("POST");
con.setRequestProperty ("Connection", "Keep-Alive");
con.setRequestProperty ("Charset", "UTF-8");
con.setRequestProperty ("Content-Type",
"Multipart / form-data; boundary =" + boundary);
DataOutputStream ds = new DataOutputStream (con.getOutputStream ());
ds.writeBytes (twoHyphens + boundary + end);
ds.writeBytes ("Content-Disposition: form-data;"
+ "Name = \" file1 \ "; filename = \" "+ file.getName () +" \ "" + end);
ds.writeBytes (end);
FileInputStream fStream = new FileInputStream (file);
int bufferSize = 1024;
byte [] buffer = new byte [bufferSize];
int length = -1;
while ((length = fStream.read (buffer))! = -1) {
ds.write (buffer, 0, length);
}
ds.writeBytes (end);
ds.writeBytes (twoHyphens + boundary + twoHyphens + end);
fStream.close ();
ds.flush ();
InputStream is = con.getInputStream ();
int ch;
StringBuffer b = new StringBuffer ();
while ((ch = is.read ())! = -1) {
b.append ((char) ch);
}
String result = b.toString ();
Log.e ("result", result);
ds.close ();
res = uploadTable (result, id, table, RequestURL);
} Catch (Exception e) {
e.printStackTrace ();
}
return res;
}
public int uploadTable (String result, int id, String table, String url) {
int res = 0;
String getUrl = url + "/ InsertTabel.action? Id =" + id + "& table ="
+ Table + "& url =" + result;
HttpClient httpclient = new DefaultHttpClient ();
try {
HttpPost post = new HttpPost (new URI (getUrl));
HttpResponse response = httpclient.execute (post);
if (response.getStatusLine (). getStatusCode () == 200
&& Response! = Null) {
HttpEntity entity = response.getEntity ();
String json = EntityUtils.toString (entity);
JSONObject obj = new JSONObject (json);
res = obj.getInt ("result");
}
} Catch (Exception e) {
e.printStackTrace ();
}
return res;
}
}
Background reading
public String upload () throws Exception
{
System.out.println ("upload");
PrintWriter pw = response.getWriter ();
int index = 0;
try
{
DiskFileUpload fu = new DiskFileUpload ();
List fileItems = null;
try
{
fileItems = fu.parseRequest (request);
System.out.println ("fileItems =" + fileItems);
Iterator iter = fileItems.iterator (); / / sequentially processing each file uploaded
while (iter.hasNext ())
{
FileItem item = (FileItem) iter.next () ;/ / ignore all forms of information other than the file domain
if (! item.isFormField ())
{
String name = item.getName () ;/ / get uploaded filename, including path
name = name.substring (name.lastIndexOf ("\ \") + 1) ;/ / extract files from the full path name
long size = item.getSize ();
if ((name == null | | name.equals ("")) && size == 0) continue;
int point = name.indexOf (".");
name = (new Date ()) getTime () + name.substring (point, name.length ()) + index;.
index + +;
File fNew = new File (getSavePath (), name);
try
{
item.write (fNew);
}
catch (Exception e)
{
e.printStackTrace ();
}
pw.write (getSavePath () + "/ /" + name);
}
else
{
String fieldvalue = item.getString (); / / if it contains Chinese should be written as: (converted to UTF-8 encoding)
/ / String fieldvalue = new String (item.getString () getBytes (), "UTF-8".);
}
}
}
catch (Exception e)
{
e.printStackTrace ();
}
}
catch (Exception e)
{
}
return SUCCESS;
}
Background printing fileItems = [], get less file documents.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
I was Httppost uploaded files packed into arguments put MultipartEntity in
String regString = null;
HttpPost httpRequest = new HttpPost (urlAdds);
try {
/ / Send out HTTP request
System.out.println ("image path ===" + imagePath);
/ / HttpRequest.setEntity (new UrlEncodedFormEntity (params, HTTP.UTF_8));
MultipartEntity entity = new MultipartEntity ();
for (Map.Entry
entity.addPart (entry.getKey (), new StringBody (entry.getValue ()));
}
if (null! = imagePath) {
System.out.println ("Image Path 1 ===" + imagePath);
File file = new File (imagePath);
entity.addPart ("authenfile", new FileBody (file));
}
httpRequest.setEntity (entity);
/ / Get HTTP response
HttpResponse httpResponse = new DefaultHttpClient () execute (httpRequest);.
/ / If the status code is 200 ok
System.out.println ("status yet =====" + httpResponse.getStatusLine () getStatusCode ().);
if (httpResponse.getStatusLine (). getStatusCode () == 200) {
isNetError = false;
/ / Register the return value
regString = EntityUtils.toString (httpResponse.getEntity ());
} Else {
Log.e ("serviceError", "the service is error!");
}
} Catch (ClientProtocolException e) {
isNetError = true;
return regString;
} Catch (UnsupportedEncodingException e) {
isNetError = true;
return regString;
} Catch (IOException e) {
isNetError = true;
return regString;
}
return regString;
}
Reply:
MultipartEntity after which version? I do not have this in 2.3.3.
Reply:
. . . . The original need to download a
Reply:
Version of the problem has nothing to do, you go to download an apache-mime4j-0.6.jar can be guided into
Reply:
Encounter the same problem, solving
No comments:
Post a Comment