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; ...@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
...@@ -133,9 +134,11 @@ public class FileController extends BaseController { ...@@ -133,9 +134,11 @@ public class FileController extends BaseController {
@Permission @Permission
@RequestMapping(value = "/download/**", method = RequestMethod.GET) @RequestMapping(value = "/download/**", method = RequestMethod.GET)
public void download(HttpServletResponse response, HttpServletRequest request) throws Exception { public void download(HttpServletResponse response, HttpServletRequest request) throws Exception {
try { String path = request.getServletPath().substring(15);
String path = request.getServletPath().substring(15); try (
IOUtils.copy(new FileInputStream(fileUploadDir + path), response.getOutputStream()); FileInputStream inputStream = new FileInputStream(fileUploadDir + path);
ServletOutputStream outputStream = response.getOutputStream();) {
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ResponseUtils.renderText(response, "File not exists!"); ResponseUtils.renderText(response, "File not exists!");
......
package com.yeejoin.amos.fas.business.controller; package com.yeejoin.amos.fas.business.controller;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
...@@ -31,64 +33,35 @@ public class WeatherController extends BaseController { ...@@ -31,64 +33,35 @@ public class WeatherController extends BaseController {
@Permission @Permission
@ApiOperation(httpMethod = "GET",value = "天气查询", notes = "天气查询") @ApiOperation(httpMethod = "GET",value = "天气查询", notes = "天气查询")
@GetMapping("/{address}") @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 = ""; String result = "";
BufferedReader in = null; try (
BufferedReader responseReader = null; GZIPInputStream gZipS=new GZIPInputStream(connection.getInputStream());
InputStreamReader res = null; InputStreamReader res = new InputStreamReader(gZipS,"UTF-8");
try { BufferedReader responseReader = new BufferedReader(res);
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();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
String readLine = new String(); 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) { while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine); sb.append(readLine);
} }
result = sb.toString(); result = sb.toString();
System.out.println(result);
} catch (Exception e) { } catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
return CommonResponseUtil.failure(e.getMessage()); 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)); return CommonResponseUtil.success(JSONObject.parse(result));
} }
......
...@@ -265,36 +265,18 @@ public class FileHelper { ...@@ -265,36 +265,18 @@ public class FileHelper {
* @param path 写入内容的文件路径 * @param path 写入内容的文件路径
*/ */
public static void writeFile(String content, String path) { public static void writeFile(String content, String path) {
OutputStream fos = null; File file = new File(path);
BufferedWriter bw = null; try (
OutputStreamWriter outputStreamWriter = null; OutputStream fos = new FileOutputStream(file);
try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
File file = new File(path); BufferedWriter bw = new BufferedWriter(outputStreamWriter);
) {
if (!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
fos = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(outputStreamWriter);
bw.write(content); bw.write(content);
} catch (FileNotFoundException fnfe) { } catch (IOException e) {
fnfe.printStackTrace(); e.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();
}
} }
} }
......
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