the request headers:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:61425
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarysqFOqeLTqOafwiDl
Cookie:JSESSIONID=F98C35E6649DC6997616A2CCB70A946A; save=F98C35E6649DC6997616A2CCB70A946A
Host:192.168.0.105:8080
Origin:http://192.168.0.105:8080
Referer:http://192.168.0.105:8080/list
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
X-Requested-With:XMLHttpRequest
this is my code:
public boolean muilt(MultipartHttpServletRequest multiReq) throws IOException {
Map<String, MultipartFile> map = multiReq.getFileMap();
List<String> list = new ArrayList<>();
for (String in : map.keySet()) {
MultipartFile str = map.get(in);
System.out.println(str.getSize());
System.out.println(str.getOriginalFilename());
if (str.getSize() != 0) {
list.add(in + "," + url + in + ".jpg");
FileOutputStream fos = new FileOutputStream(new File(url + in + ".jpg"));
FileInputStream fs = (FileInputStream) str.getInputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fs.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
fs.close();
}
}
if (list.size() < 7){
return false;
}
for (int i = 0; i < 6; ++i){
String[] sourceStrArray = list.get(i).split(",");
this.split(sourceStrArray[0], sourceStrArray[1]);
}
return true;
}
the error code :
FileInputStream fs = (FileInputStream) str.getInputStream();
when it run, I get the problem:
java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be cast to java.io.FileInputStream at tools.SplitPhoto.muilt(SplitPhoto.java:87) at controller.HanderPhoto.photoHander(HanderPhoto.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
the source is a picture,I want to write it's stream to the file, how should I do?
MultipartFile's getInputStream() method returns an InputStream. You don't have to know what kind of InputStream it returns. As you see, it's not a FileInputStream, and that should not matter.
All you need to do is read from the InputStream returned and write to your file. You read from an InputStream the same way, whatever the concrete type of the InputStream is. That's what polymorphism is all about.
So, just remove that useless cast, and use InputSTream instead of FileInputStream.
Note that you can copy all the content of an InputStream to a file in one line of code using Files.