I have a server written in Go
.
Basically, it receives POST-request and sends some file in response as multipart/form-data.
Following is the server code:
func ColorTransferHandler(w http.ResponseWriter, r *http.Request) {
... some routine...
w.WriteHeader(http.StatusOK)
mw := multipart.NewWriter(w)
filename := "image_to_send_back.png"
part, err := mw.CreateFormFile("image", filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
local_file, err := os.Open(filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
local_file_content, err := ioutil.ReadAll(local_file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
part.Write(local_file_content)
w.Header().Set("Content-Type", mw.FormDataContentType())
if err := mw.Close(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
Client code in Java
for Android:
public class ConnectionUtility {
private final String boundary;
private static final String LINE_FEED = "rn";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
public List<String> finish() throws IOException {
// ...
// content filling
// ...
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
List<String> response = new ArrayList<String>();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
Following are my questions:
1. How do I edit this function to get that image file and save it on a drive?
2. Probably I can use some libraries to do this?
Help will be appreciated