|
|
@@ -27,7 +27,6 @@ import java.net.URLEncoder;
|
|
|
import javax.servlet.ServletContext;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import com.jfinal.kit.LogKit;
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
|
/**
|
|
|
@@ -184,7 +183,7 @@ public class FileRender extends Render {
|
|
|
outputStream.write(buffer, 0, len);
|
|
|
}
|
|
|
outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
+
|
|
|
} catch (IOException e) { // ClientAbortException、EofException 直接或间接继承自 IOException
|
|
|
String name = e.getClass().getSimpleName();
|
|
|
if (name.equals("ClientAbortException") || name.equals("EofException")) {
|
|
|
@@ -194,8 +193,8 @@ public class FileRender extends Render {
|
|
|
} catch (Exception e) {
|
|
|
throw new RenderException(e);
|
|
|
} finally {
|
|
|
- if (inputStream != null)
|
|
|
- try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
|
|
|
+ close(inputStream);
|
|
|
+ close(outputStream);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -217,8 +216,9 @@ public class FileRender extends Render {
|
|
|
long start = range[0];
|
|
|
long end = range[1];
|
|
|
inputStream = new BufferedInputStream(new FileInputStream(file));
|
|
|
- if (inputStream.skip(start) != start)
|
|
|
- throw new RuntimeException("File skip error");
|
|
|
+ if (inputStream.skip(start) != start) {
|
|
|
+ throw new RuntimeException("File skip error");
|
|
|
+ }
|
|
|
outputStream = response.getOutputStream();
|
|
|
byte[] buffer = new byte[1024];
|
|
|
long position = start;
|
|
|
@@ -235,21 +235,18 @@ public class FileRender extends Render {
|
|
|
}
|
|
|
}
|
|
|
outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
- catch (IOException e) { // ClientAbortException、EofException 直接或间接继承自 IOException
|
|
|
+
|
|
|
+ } catch (IOException e) { // ClientAbortException、EofException 直接或间接继承自 IOException
|
|
|
String name = e.getClass().getSimpleName();
|
|
|
if (name.equals("ClientAbortException") || name.equals("EofException")) {
|
|
|
} else {
|
|
|
throw new RenderException(e);
|
|
|
}
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
throw new RenderException(e);
|
|
|
- }
|
|
|
- finally {
|
|
|
- if (inputStream != null)
|
|
|
- try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
|
|
|
+ } finally {
|
|
|
+ close(inputStream);
|
|
|
+ close(outputStream);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -263,20 +260,23 @@ public class FileRender extends Render {
|
|
|
protected void processRange(Long[] range) {
|
|
|
String rangeStr = request.getHeader("Range");
|
|
|
int index = rangeStr.indexOf(',');
|
|
|
- if (index != -1)
|
|
|
+ if (index != -1) {
|
|
|
rangeStr = rangeStr.substring(0, index);
|
|
|
+ }
|
|
|
rangeStr = rangeStr.replace("bytes=", "");
|
|
|
|
|
|
String[] arr = rangeStr.split("-", 2);
|
|
|
- if (arr.length < 2)
|
|
|
+ if (arr.length < 2) {
|
|
|
throw new RuntimeException("Range error");
|
|
|
+ }
|
|
|
|
|
|
long fileLength = file.length();
|
|
|
for (int i=0; i<range.length; i++) {
|
|
|
if (StrKit.notBlank(arr[i])) {
|
|
|
range[i] = Long.parseLong(arr[i].trim());
|
|
|
- if (range[i] >= fileLength)
|
|
|
+ if (range[i] >= fileLength) {
|
|
|
range[i] = fileLength - 1;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -291,8 +291,9 @@ public class FileRender extends Render {
|
|
|
}
|
|
|
|
|
|
// check final range
|
|
|
- if (range[0] == null || range[1] == null || range[0].longValue() > range[1].longValue())
|
|
|
+ if (range[0] == null || range[1] == null || range[0].longValue() > range[1].longValue()) {
|
|
|
throw new RuntimeException("Range error");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|