我已经构建了将文件上传到文件夹的Web服务,并且在使用postman进行测试时工作正常,但是当我从java应用程序或android应用程序调用此Web服务时,它会抛出异常" Stream意外结束&# 34 ;. 这是抛出异常的堆栈跟踪
org.apache.tomcat.util.http.fileupload.MultipartStream $ MalformedStreamException: 流意外结束了 org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:520) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.http.fileupload.FileUploadBase $ FileItemIteratorImpl.findNextItem(FileUploadBase.java:880) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.http.fileupload.FileUploadBase $ FileItemIteratorImpl。(FileUploadBase.java:845) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.Request.parseParts(Request.java:2868) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.Request.parseParameters(Request.java:3216) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.Request.getParameter(Request.java:1137) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) 〜[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) 〜[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 〜[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 〜[tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.29.jar:8.5.29] at java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) [na:1.8.0_161] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) [NA:1.8.0_161]
这是web服务src代码: @RestController 公共类FileUploadController { private static final String UPLOAD_FOLDER =" C:\ Users \ Sagar \ Uploads";
@RequestMapping(value="/upload",headers ="content-type=multipart/form-data",method=RequestMethod.POST)
public String uploadFile(@RequestParam("file")MultipartFile multipartFile)
{
if(multipartFile.isEmpty())
{
return "Please select a file to upload";
}
saveFile(multipartFile);
return "Successfully uploaded "+multipartFile.getOriginalFilename();
}
private void saveFile(MultipartFile multipartFile)
{
try
{
byte[] bytes=multipartFile.getBytes();
String data=new String(bytes);
System.out.println(data);
Path path=Paths.get(UPLOAD_FOLDER+File.separator+multipartFile.getOriginalFilename()+File.separator);
Files.write(path, bytes);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是java代码
公共类客户端 {
private static final int B = 0;
private static Object uploaded_file;
public static void main(String[] args)
{
String urlString="http://192.168.1.5:8888/upload";
HttpURLConnection connection;
String filePath="C:\\Users\\Sagar\\Uploads\\025_program.txt";
FileInputStream fileInputStream = null;
String boundary="WebKitFormBoundary7MA4YWxkTrZu0gW";
String linrEnd="\r\n" ;
String twoHyphens = "--";
int maxBufferSize = 1 * 1024 * 1024;
try
{
URL url=new URL(urlString);
try
{
fileInputStream = new FileInputStream(filePath);
connection=(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
connection.setRequestProperty("Accept", "text/plain");
ByteArrayOutputStream bs=new ByteArrayOutputStream();
bs.write((twoHyphens+boundary+linrEnd).getBytes());
bs.write(("Content-Disposition: form-data; file="+"Program.txt").getBytes());
bs.write(linrEnd.getBytes());
bs.write(("Content-Type:text/plain").getBytes());
bs.write((linrEnd).getBytes());
try
{
int bytesAvailable=fileInputStream.available();
int bufferSize=Math.min(bytesAvailable,maxBufferSize);
byte[] buffer=new byte[bufferSize];
int bytesRead=fileInputStream.read(buffer,0,bufferSize);
while(bytesRead>0)
{
bs.write(buffer,0,bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
bs.write((twoHyphens+boundary+twoHyphens).getBytes());
String dataOutput=new String(bs.toByteArray());
System.out.println(dataOutput);
OutputStream outputStream=new DataOutputStream(connection.getOutputStream());
byte[] out=bs.toByteArray();
outputStream.write(out);
outputStream.close();
}
catch (FileNotFoundException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
outputStream.flush();
outputStream.close();
int responseCode=connection.getResponseCode();
String responseMessage=connection.getResponseMessage();
if (responseCode==200)
{
System.out.println("File Uploaded Successfully");
}
else
{
System.out.println("Response Code:- "+responseCode+"\n"+responseMessage);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (MalformedURLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}