81,090
社区成员
发帖
与我相关
我的任务
分享
<html:form action="/chapter/chapter.do" method="POST"
enctype="multipart/form-data">
<td colspan="4">章节文件<font color="#FF0000">*</font> <input type="file" name="formFile" contenteditable="false"
style="width:500"></td>
</html:form>
private FormFile formFile = null;
public FormFile getFormFile() {
return formFile;
}
public void setFormFile(FormFile formFile) {
this.formFile = formFile;
}
action:
try {
ChapterObj chapterObj = chapterForm.getChapterObj();
SeriesObj seriesObj = chapterForm.getSeriesObj();
String filePath = seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/"
+ chapterObj.getChapterNo()
+ COMICCFG.STORE.CHAPTER_EXT;
chapterObj.setSeriesId(seriesObj.getSeriesId());
chapterObj.setFilePath(filePath);
try {
// 获取存储根路径
String storePath = GlobalInfo.getProperty(
COMICCFG.STORE.comic_content, COMICCFG.STORE.app,
COMICCFG.STORE.root_dir);
if (storePath == null || storePath.equals("")) {
throw new ComicException("取存储根路径失败!");
}
// 取上传后的文件
FormFile formFile = chapterForm.getFormFile();
if (formFile == null || formFile.getFileName().equals("")) {
throw new ComicException("取上传后的章节文件失败!");
}
String storeDir = storePath + seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/";
// 存文件
FileUpload.storeFile(formFile, storeDir, chapterObj
.getChapterNo());
// 取操作员ID
String operatorId = ((AdminUserForm) request.getSession()
.getAttribute("ADMIN_USER_INFORMATION"))
.getAdminUser().getUserId();
// 插入数据库
ChapterDAO.getInstance().insertChapter(chapterObj,
operatorId);
chapterObj.setChapterNo(String.valueOf(Integer
.parseInt(chapterObj.getChapterNo()) + 1));
} catch (ComicException e) {
if (e.getMessage().contains("ORA-00001")) {
throw new ComicException(" “章节序号-分辨率”重复!");
} else {
throw e;
}
} catch (Exception e) {
throw e;
}
/**
* 存储文件的操作类
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.struts.upload.FormFile;
public class FileUpload {
/**
* 将form接收到的文件存储到目标路径,文件名不变,文件类型后缀改为大写字母
*
* @param formFile
* action中form接受到的文件对象
* @param storeDir
* 目标路径
* @return 实际存储的文件全路径。
* @throws Exception
*/
public static String storeFile(FormFile formFile, String storeDir)
throws Exception {
String fileName = formFile.getFileName();
String fileExtension = "";
String filePath = "";
try {
if (fileName.length() == 0) {
throw new Exception("没有选择文件!");
}
// 取扩展名
if (fileName.lastIndexOf(".") > 0) {
fileExtension = fileName.substring(
fileName.lastIndexOf(".") + 1, fileName.length());
}
// 取文件名
String[] possibleSeparator = new String[] { "/", "\\" };
for (int i = 0; i < possibleSeparator.length; i++) {
String separator = possibleSeparator[i];
int index = fileName.lastIndexOf(separator);
if (index != -1) {
fileName = fileName.substring(index + separator.length());
break;
}
}
// 如果目录不存在,则创建目录
File f = new File(storeDir);
if (!f.exists()) {
f.mkdirs();
}
} catch (Exception e) {
throw e;
}
try {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
filePath = storeDir + fileName + "." + fileExtension.toLowerCase();
// 如果文件已存在,则删掉
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(formFile.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(new File(
filePath)));
byte[] bytes = new byte[2048];
int s = 0;
int success = 0;
while ((s = in.read(bytes)) != -1) {
out.write(bytes, 0, s);
success++;
}
if (success == 0) {
throw new Exception("文件读取错误!");
}
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
try {
out.close();
} catch (IOException e) {
// ignore
}
}
} catch (Exception ex) {
throw new ServletException("FileItem.write() fail", ex);
}
return filePath;
}
/**
* 将form接收到的文件存储到目标路径,文件名按输入的文件名,文件类型后缀改为大写字母
*
* @param formFile
* action中form接受到的文件对象
* @param storeDir
* 目标路径
* @param fileName
* 输入的文件名
* @return 实际存储的文件全路径。
* @throws Exception
*/
public static String storeFile(FormFile formFile, String storeDir, String fileName)
throws Exception {
String oldFileName = formFile.getFileName();
String fileExtension = "";
String filePath = "";
try {
if (fileName.length() == 0) {
throw new Exception("没有选择文件!");
}
// 取扩展名
if (oldFileName != null && oldFileName.lastIndexOf(".") > 0) {
fileExtension = oldFileName.substring(oldFileName
.lastIndexOf(".") + 1, oldFileName.length());
}
// 取文件名
String[] possibleSeparator = new String[] { "/", "\\" };
for (int i = 0; i < possibleSeparator.length; i++) {
String separator = possibleSeparator[i];
int index = fileName.lastIndexOf(separator);
if (index != -1) {
fileName = fileName.substring(index + separator.length());
break;
}
}
// 如果目录不存在,则创建目录
File f = new File(storeDir);
if (!f.exists()) {
f.mkdirs();
}
} catch (Exception e) {
throw e;
}
try {
filePath = storeDir + fileName + "." + fileExtension.toLowerCase();
// 如果文件已存在,则删掉
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(formFile.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(new File(
filePath)));
byte[] bytes = new byte[2048];
int s = 0;
int success = 0;
while ((s = in.read(bytes)) != -1) {
out.write(bytes, 0, s);
success++;
}
if (success == 0) {
throw new Exception("文件读取错误!");
}
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
try {
out.close();
} catch (IOException e) {
// ignore
}
}
} catch (Exception ex) {
throw new ServletException("FileItem.write() fail", ex);
}
return filePath;
}
}