-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryFileBody.java
86 lines (56 loc) · 1.82 KB
/
BinaryFileBody.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.*;
/**
* This class set a file to request body
* for use : first call {@link BinaryFileBody#build()} then call {@link BinaryFileBody#set(OutputStream)}.
* Use {@link BinaryFileBody#getContentType()} and {@link BinaryFileBody#getContentLength()} to set the Content headers
*
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.2.0
*/
public class BinaryFileBody extends RequestBody
{
/* Fields */
// file address
private String filePath;
// file that we want to send it as request body
private transient FileInputStream bodyFile;
private static final long serialVersionUID = 775061443934725L;
/* Constructor */
/**
* Create a new binary file body for request
*
*
* @param filePath : address of the file that you want to send it as your request body
*/
public BinaryFileBody(String filePath)
{
this.filePath = filePath;
}
/* Methods */
/**
* This method open your given file and make it ready for send as request body
*/
public void build()
{
// check that given path is available or not
try { bodyFile = new FileInputStream(filePath); }
catch (FileNotFoundException e) { error(); }
}
/**
* This method set the file as request body
*/
public void set(OutputStream connectionOutputStream) throws IOException
{
BufferedInputStream fileInputStream = new BufferedInputStream(bodyFile);
connectionOutputStream.write(fileInputStream.readAllBytes());
super.Content_Length = fileInputStream.readAllBytes().length;
connectionOutputStream.flush();
connectionOutputStream.close();
}
public String getContentType()
{
return "application/octet-stream";
}
}