//アクセスメソッドを使用してFormFileオブジェクトの取得
FormFile fileUp = upForm.getImage();
// DB格納用ファイル名
String fileNameDB = "";
//入力フォームからの画像ファイル指定がなければ以下の処理を実施
if (fileUp.getFileSize() != 0) {
// getInputStreamメソッドを使用し、入力ストリームを取得
InputStream is = null;
ImageInputStream stream = null;
ImageReader reader = null;
BufferedImage image = null;
try {
is = fileUp.getInputStream();
stream =
ImageIO.createImageInputStream(newByteArrayInputStream(fileUp.getFileData()));
reader =
ImageIO.getImageReadersByMIMEType("image/jpeg").next();
reader.setInput(stream);
image = reader.read(0);
i
f ((200 < image.getWidth()) || (100
< image.getHeight())) {
throw new Exception("画像ファイルのサイズが不正");
}
} catch (FileNotFoundException e1) {
throw new Exception(e1.getMessage(), e1);
} catch (IOException e1) {
throw new Exception(e1.getMessage(), e1);
} finally {
if (reader != null) {
reader.dispose();
}
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
throw new Exception(e.getMessage(), e);
}
}
}
// イメージファイル名の取得
String fileName = fileUp.getFileName();
// 拡張子の取得
int index = fileName.lastIndexOf(".");
String extension = null;
if (index != -1) {
extension = fileName.substring(index);
}
ConfigProperties config = ConfigProperties.getInstance();
String tomCatPath = config.getProperty("pathNameTomcat");
String imagePath = config.getProperty("pathNameImage");
// DBに入れるファイル名(トムキャットのwebapps +
SP-ID+拡張子)
fileNameDB = imagePath + newCP.getId() + extension;
// イメージファイルの配置場所
String abImagePath = tomCatPath + fileNameDB;
//ファイルのアップロード先を指定して、出力ストリームを生成
FileOutputStream fos = null;
try {
fos = new FileOutputStream(abImagePath);
} catch (FileNotFoundException e1) {
throw new ASPBusinessException(e1.getMessage(), e1);
}
// 出力ストリームをバッファリング
BufferedOutputStream outBuffer = new BufferedOutputStream(fos);
int contents = 0;
// 入力ストリームをバッファリング
BufferedInputStream inBuffer = new BufferedInputStream(is);
// 入力データがなくなるまで入出力処理を実行
try {
while ((contents = inBuffer.read()) != -1) {
outBuffer.write(contents);
outBuffer.flush();
}
} catch (IOException e1) {
throw new Exception(e1.getMessage(), e1);
} finally {
try {
inBuffer.close();
outBuffer.close();
} catch (IOException e) {
throw new Exception(e.getMessage(), e);
}
// 一時領域のアップロードデータを削除
fileUp.destroy();
}
}
最終更新:2007年03月05日 17:19