Skip to content

Instantly share code, notes, and snippets.

@masatomix
Created February 26, 2019 06:39
Show Gist options
  • Save masatomix/9796f56c5710b1e3f25368eb9194c8c1 to your computer and use it in GitHub Desktop.
Save masatomix/9796f56c5710b1e3f25368eb9194c8c1 to your computer and use it in GitHub Desktop.
Upload
package nu.mine.kino.springboot;
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/bin")
public class SampleController {
@ResponseBody
@CrossOrigin
@RequestMapping("/bin1")
public byte[] getProjectByMultipartFile(
@RequestParam("upload_file") MultipartFile multipartFile)
throws IOException {
byte[] buf = multipartFile.getBytes();
return buf;
}
@ResponseBody
@CrossOrigin
@RequestMapping(path = "/bin2", consumes = {
MediaType.APPLICATION_OCTET_STREAM_VALUE })
public byte[] getProjectByOctetStream(ServletRequest request)
throws IOException {
InputStream in = request.getInputStream();
byte[] buf = stream2Byte(in);
return buf;
}
private static byte[] stream2Byte(InputStream in) throws IOException {
byte[] pix = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1) {
baos.write(b, 0, j);
}
pix = baos.toByteArray();
} finally {
if (in != null) {
in.close();
}
}
return pix;
}
}
//curl -X POST \
//-F 'upload_file=@/Users/masatomix/git/myproject/pom.xml' \
//http://192.168.2.250:8085/bin/bin1
//
//
//curl -X POST \
//--data-binary '@/Users/masatomix/git/myproject/pom.xml' \
//-H 'Content-type: application/octet-stream' \
//http://192.168.2.250:8085/bin/bin2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment