Skip to content

Instantly share code, notes, and snippets.

@KohsukeHada
Last active August 29, 2015 13:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save KohsukeHada/9974163 to your computer and use it in GitHub Desktop.
ローカル サーバ内のファイル サイズを返す JSP コード。jquery.file.size-extension.js と組み合わせて使用。
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="WINDOWS-31J" %>
<%
//ローカル サーバ内のファイル サイズを返す
String callbackFunc = request.getParameter("callback");
String DOWNLOAD_FILE_URL = request.getParameter("url");
try {
java.net.URL url = new java.net.URL (DOWNLOAD_FILE_URL);
java.lang.String root = "/http_root_dir/"; //URLのルートにあたるサーバのローカル ディレクトリ
java.lang.String path = url.getPath();
java.lang.String path = root + path;
java.io.FileInputStream file = new java.io.FileInputStream(path);
int file_size = file.available();
file.close();
out.println(callbackFunc + "({size:\"" + file_size + "\"});");
// //URLクラスでアクセスする方法だと認証が必用なディレクトリのサイズが取得できない
// java.net.HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) url . openConnection ();
//
// httpURLConnection . setRequestMethod ("HEAD");
// httpURLConnection . connect ();
// if( httpURLConnection . getResponseCode () == 200 ) {
// out.println(callbackFunc + "({size:\"" + httpURLConnection . getContentLength () + "\"});");
// }
// else {
// out.println(callbackFunc + "({size:\"" + -1 + "\"});");
// }
}
catch (IOException e) {
out.println(callbackFunc + "({size:\"" + -99 + "\"});");
}
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment