Commit 1198a5a4 authored by litengwei's avatar litengwei

Merge remote-tracking branch 'origin/develop_dl_bugfix' into develop_dl_bugfix

parents 6447479d c2e52969
......@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
......@@ -133,9 +134,11 @@ public class FileController extends BaseController {
@Permission
@RequestMapping(value = "/download/**", method = RequestMethod.GET)
public void download(HttpServletResponse response, HttpServletRequest request) throws Exception {
try {
String path = request.getServletPath().substring(15);
IOUtils.copy(new FileInputStream(fileUploadDir + path), response.getOutputStream());
String path = request.getServletPath().substring(15);
try (
FileInputStream inputStream = new FileInputStream(fileUploadDir + path);
ServletOutputStream outputStream = response.getOutputStream();) {
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
ResponseUtils.renderText(response, "File not exists!");
......
package com.yeejoin.amos.fas.business.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;
......@@ -31,64 +33,35 @@ public class WeatherController extends BaseController {
@Permission
@ApiOperation(httpMethod = "GET",value = "天气查询", notes = "天气查询")
@GetMapping("/{address}")
public CommonResponse getWeather(@PathVariable("address") String address) {
public CommonResponse getWeather(@PathVariable("address") String address) throws IOException {
String urlNameString = weatherUrl + address;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setUseCaches(false);
// 建立实际的连接
connection.connect();
String result = "";
BufferedReader in = null;
BufferedReader responseReader = null;
InputStreamReader res = null;
try {
String urlNameString = weatherUrl + address;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// connection.setRequestProperty("Charset", "utf-8");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setUseCaches(false);
// 建立实际的连接
connection.connect();
try (
GZIPInputStream gZipS=new GZIPInputStream(connection.getInputStream());
InputStreamReader res = new InputStreamReader(gZipS,"UTF-8");
BufferedReader responseReader = new BufferedReader(res);
) {
StringBuffer sb = new StringBuffer();
String readLine = new String();
GZIPInputStream gZipS=new GZIPInputStream(connection.getInputStream());
res = new InputStreamReader(gZipS,"UTF-8");
responseReader = new BufferedReader(res);
while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine);
}
result = sb.toString();
System.out.println(result);
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
return CommonResponseUtil.failure(e.getMessage());
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
if (null != responseReader) {
responseReader.close();
}
if (null != res) {
res.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return CommonResponseUtil.success(JSONObject.parse(result));
}
......
......@@ -265,36 +265,18 @@ public class FileHelper {
* @param path 写入内容的文件路径
*/
public static void writeFile(String content, String path) {
OutputStream fos = null;
BufferedWriter bw = null;
OutputStreamWriter outputStreamWriter = null;
try {
File file = new File(path);
File file = new File(path);
try (
OutputStream fos = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(outputStreamWriter);
) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(outputStreamWriter);
bw.write(content);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (null != bw) {
bw.close();
}
if (null != fos) {
fos.close();
}
if (null != outputStreamWriter) {
outputStreamWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment