Java HTTP

响应

JSON Response

1
2
3
4
5
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("[]");
out.flush();

上传文件

HttpClient

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
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(postUrl);
MultipartEntity mpEntity = new MultipartEntity();
Iterator<Map.Entry<String, Object>> iter = formParams.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Object> entry = iter.next();
String inputName = entry.getKey();
String inputValue = entry.getValue().toString();
mpEntity.addPart(inputName, new StringBody(inputValue, Charset.defaultCharset()));
}
mpEntity.addPart("file", new FileBody(file, "image/jpeg"));
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response2 = null;
response2 = httpclient.execute(httppost);

HttpEntity resEntity = response2.getEntity();
System.out.println(response2.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
httpclient.getConnectionManager().shutdown();
} catch (IOException e) {
e.printStackTrace();
}

下载文件

Java IO

1
2
3
4
5
6
7
8
9
10
try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
// handle exception
}

Java 7

1
2
InputStream in = new URL(FILE_URL).openStream();
Files.copy(in, Paths.get(FILE_NAME), StandardCopyOption.REPLACE_EXISTING);

Read contents as String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static String URLReader(URL url) throws IOException {
StringBuilder sb = new StringBuilder();
String line;

InputStream in = url.openStream();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
} finally {
in.close();
}

return sb.toString();
}

Java NIO

The transferTo() and transferFrom() methods are more efficient than simply reading from a stream using a buffer. Depending on the underlying operating system, the data can be transferred directly from the filesystem cache to our file without copying any bytes into the application memory.

On Linux and UNIX systems, these methods use the zero-copy technique that reduces the number of context switches between the kernel mode and user mode.

1
2
3
4
URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Apache Commons IO

1
FileUtils.copyURLToFile(new URL(FILE_URL), new File(FILE_NAME), CONNECT_TIMEOUT, READ_TIMEOUT);

判断文件类型

Java IO & NIO - Files.probeContentType()

1
2
3
4
path = Files.createTempFile("test-file", ".html");
System.out.println("Probe Content Type for file: " + path);
type = Files.probeContentType(path);
System.out.println("File Content type: " + type);

Determining a File Type In Java

MimetypesFileTypeMap.getContentType(String)

1
2
final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
fileTypeMap.getContentType(fileName);

URLConnection.getContentType()

1
2
3
final URL url = new URL("file://" + fileName);
final URLConnection connection = url.openConnection();
fileType = connection.getContentType();

Apache Tika

1
2
3
Tika tika = new Tika(); 
tika.detect(istream);
tika.detect(file);