我有一个Android应用程序和我的魔杖从这个应用程序,大图像(在这种情况下32MB)上传到Spring Server,但我得到java.net.SocketException:sendto失败:EPIPE(断管)错误。
我使用这种方法:
public static void setPicture(User user, Picture picture, HavePicture havePicture, File file) {
try {
URL url = new URL(Request.BASE_URL + Request.BASE_PATH + Request.GAME_SYSTEM_PATH + Request.SET_PICTURE_PATH);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true); // indicates POST method
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setRequestProperty("User-Agent", "CodeJava Agent");
connection.setRequestProperty("Test", "Bonjour");
//load file with multi small pices not one lage pice good to use large filse
connection.setChunkedStreamingMode(1024);
OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, Request.CHARSET), true);
addFormField(writer, Request.PARAM_USER, Json.toJson(user));
addFormField(writer, Request.PARAM_PICTURE, Json.toJson(picture));
addFormField(writer, Request.PARAM_HAVE_PICTURE, Json.toJson(havePicture));
addFilePart(writer, outputStream, Request.PARAM_FILE, file);
StringBuffer response = new StringBuffer();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
String a = response.toString();
System.out.println(a);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
params:user
,picture
和havePicture
是我想要用大图像发送json的一些对象,这种方法适用于小于1MB的小图像。
这里是其他方法:
private static void addFormField(PrintWriter writer, String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=" + Request.CHARSET).append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
用于图像文件:
private static void addFilePart(PrintWriter writer,OutputStream outputStream, String fieldName, File uploadFile) throws IOException {
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE_FEED);
writer.append("Content-Type: "+URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
FileInputStream inputStream = new FileInputStream(uploadFile);
writer.append("Content-length: "+inputStream.available()).append(LINE_FEED);
System.out.println("- - "+"Content-length: "+inputStream.available());
writer.append(LINE_FEED);
writer.flush();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
这是我完成的错误:
java.net.SocketException:sendto failed:在java的libcore.io.IoBridge.sendto(IoBridge.java:555)的libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:586)上的EPIPE(Broken pipe)。 net.PlainSocketImpl.write(PlainSocketImpl.java:520)at java.net.PlainSocketImpl.access $ 100(PlainSocketImpl.java:43)at java.net.PlainSocketImpl $ PlainSocketOutputStream.write(PlainSocketImpl.java:272)at com.android。 ok.com.Okio $ 1.write(Okio.java:70)com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)at com.android.okio.RealBufferedSink.write(RealBufferedSink.java:44)at com。 android.okhttp.internal.http.HttpConnection $ ChunkedSink.write(HttpConnection.java:334)at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)at com.android.okio.RealBufferedSink $ 1.write(RealBufferedSink) .java:131)com.mayan.ameritrade.android.tools.Server $ override.addFilePart(Server.java:435)at com.mayan.ameritrade.android.tools.Server $ override.access $ dispatch(Server.jav) a)位于java.lang.reflect.Method.invoke的java.lang.reflect.Method.invoke(Native Method)中的com.mayan.ameritrade.android.tools.Server.addFilePart(Server.java:0)(方法。 java:372)com的com.andyan.amesrade.android.tools.Server $ override.setPicture(Server.java:363)com的com.android.tools.fd.runtime.AndroidInstantRuntime.invokeProtectedStaticMethod(AndroidInstantRuntime.java:170) .mayan.ameritrade.andrit.tools.Server $ override.access $ dispatch(Server.java)at com.mayan.ameritrade.android.tools.Server.setPicture(Server.java:0)at com.mayan.ameritrade.android .MainActivity $ 2 $ 1.run(MainActivity.java:99)at java.lang.Thread.run(Thread.java:818)引起:android.system.ErrnoException:sendto failed:libcore.io上的EPIPE(Broken pipe)。位于libcore.io.Posix.sendto的libcore.io.Posix.sendto(Posix.java:206)上的Posix.sendtoBytes(Native方法),libcore.io.IoBridge.sendto上的libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)(IoBridge.java: 553)...再多20个
我现在该做什么,谢谢你的帮助!
答案 0 :(得分:0)
你无法使用PrintWriter上传图片......好吧,你不......但......
PrintWriter仅用于文本。
您正在混合PrintWriter和普通的OutputStream。那不行。
您应该只写一种类型的流。
答案 1 :(得分:0)
我的问题解决了!感谢@Randyka Yudhistira和@greenapps的帮助, 我的问题是客户端中的一些不必要的代码,主要是我的Spring服务器无法上传大文件
<强>解决方案强>
添加inserver端:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSizePerFile">yourMaxSizeToUpload</property>
</bean>
或
@Bean
public CommonsMultipartResolver getCommonsMultipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(yourMaxSizeToUpload);
return resolver;
}
如果这不起作用,请尝试删除不必要的代码,如:
writer.append("Content-length: "+inputStream.available()).append(LINE_FEED);
就我而言。