1

私はTomcatが自分自身や他の人にとって危険であることを十分に知っています.

デプロイするには、WAR ファイル (Eclipse からエクスポート) を作成し、その WAR ファイルを \apache-tomcat-7.0.23\webapps\ フォルダーにコピーしてから、\apache-tomcat-7.0.23\bin\startup を使用して再起動します。バット。

私の webapp は PDF ファイルをダウンロード フォルダーに書き込みます。これは、私の Eclipse プロジェクトの WebContent フォルダーにある必要があることを理解しています。

問題は、新しい WAR ファイルをインストールすると、Tomcat がそれを解凍すると、ダウンロード フォルダーの内容が消去されるため、ユーザーの以前の出力が失われることです。

お時間とご協力いただきありがとうございます。

4

2 に答える 2

2

I think, it's not right (at least, not very convenient) to save user files to WebContent folder of the web application. WebContent folder is meant for resources that are distributed inside the web application archive.

It's common practice to use some specific location on disk for user files that are planned to be reused.

1) To specify the location in configuration file, create file app.properties in the root of application CLASSPATH. In this file you will have the property

user.pdfs.location=/path/to/user/pdfs

To read the property use the following code in your servlet:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream propsStream = classLoader.getResourceAsStream("app.properties");

Properties properties = new Properties();
properties.load(propsStream);

String userPdfsPath = properties.getProperty("user.pdfs.location");

So you'll always be able to change location of PDFs in "/WEB-INF/classes/app.properties" file in already created WAR.

2) Or you may pass parameter to the Tomcat's JVM:

-Duser.pdfs.location="/path/to/user/pdfs"

To read value of the parameter use the following code:

String userPdfsPath = System.getProperty("user.pdfs.location");

Another popular (but not always right) approach is to save files to database as BLOBs. Consider that in that case you sometimes may have problems with application perfomance or backups.

If you want to work with temporary files (for example, you want to create PDFs for users just to download) you should use JVM's temp directory for that. To get temp directory use

String tempDirPath = System.getProperty("java.io.tmpdir");

Or simply create temp PDF file with

File tempPDF = File.createTempFile("temp-", ".pdf");
于 2012-11-13T06:09:36.933 に答える
1

アプリケーションを再デプロイするたびに、war ファイルと抽出されたフォルダーの両方が Tomcat によって消去されます。そのため、抽出された war パス内に何かを保存している場合は、次回の再デプロイ時に削除されます。また、通常、抽出された Web アプリケーションにユーザー データを格納することはお勧めできません。ユーザーファイルを作成するために他のパスを選択した方が便利です。

于 2012-11-13T16:25:28.583 に答える