私は、サーバーマシン自体のフォルダから画像をアップロードして取得する方法を学び実装しています。しかし、私はそれを自分のwebappのディレクトリにあるフォルダに実装しています。 image
サーブレットのパスが/image/*
の場合、context
のパスをserver.xml
に設定したディレクトリのフォルダにアクセスできません。 コードは以下のとおりです。 Image.java servlet
:Tomcatで自動的に新しいフォルダを作成し、アップロードしたデータを保存するにはどうすればいいですか?
public class Image extends HttpServlet {
public static void returnFile(String filename, OutputStream out)
throws FileNotFoundException, IOException {
// A FileInputStream is for bytes
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = fis.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
} finally {
if (fis != null) {
fis.close();
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Use a ServletOutputStream because we may pass binary information
ServletOutputStream out = response.getOutputStream();
// Get the file to view
String file = request.getPathTranslated();
// No file, nothing to view
if (file == null) {
out.println("No file to view");
return;
}
// Get and set the type of the file
String contentType = getServletContext().getMimeType(file);
response.setContentType(contentType);
// Return the file
try {
returnFile(file, out);
} catch (Exception ex) {
System.out.print("\n" + ex.getMessage() + "\n");
ex.printStackTrace();
}
}
}
context variable:
<Context path="*/photos/users" docBase="G:\myApp\images\users" debug="0" reloadable="true" />
私が欲しいもの任意のパスにで私のWebアプリケーションに近い/データ/ユーザーよう何かを与えることです絶対パスの場所G:\ myApp \ images \ usersどのTomcat 6.x/7.xサーバマシン上でもwebappの最初のデプロイ時に動的に作成され、その中のデータにアクセスしたいときはいつでも使用できます。 私は私の質問ではっきりしていることを願っています。
これは、サーブレットの仕様。マルチホストや別のコンテナを使用するときに問題が発生する可能性があります。 –