67,513
社区成员
发帖
与我相关
我的任务
分享
(java.util.HashMap<K,V>)
{bookFile=[Ljava.io.File;@e40293,
submit=[Ljava.lang.String;@1652020,
bookFileContentType=[Ljava.lang.String;@2023f3,
bookId=[Ljava.lang.String;@1854be,
bookFileFileName=[Ljava.lang.String;@163c17d}
private File bookFile;
private String bookFileContentType;
private String bookFileFileName;
private String bookId;
@Component("bookUploadAction")
public class RBookUploadAction extends ActionSupport implements RequestAware, ServletResponseAware{
private static final long serialVersionUID = -3430678334134919673L;
private RequestMap request;
private HttpServletResponse response;
private File bookFile;
private String bookFileContentType;
private String bookFileFileName;
private String bookId;
@Resource(name="bookMgmtServiceImpl")
private IRBookMgmtService iBookService;
PhoenixProperties phoenixProp = PhoenixProperties.getInstance();
public void setiBookService(IRBookMgmtService iBookService) {
this.iBookService = iBookService;
}
public File getBookFile() {
return bookFile;
}
public void setBookFile(File bookFile) {
this.bookFile = bookFile;
}
public String getBookFileContentType() {
return bookFileContentType;
}
public void setBookFileContentType(String bookFileContentType) {
this.bookFileContentType = bookFileContentType;
}
public String getBookFileFileName() {
return bookFileFileName;
}
public void setBookFileFileName(String bookFileFileName) {
this.bookFileFileName = bookFileFileName;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String uploadBook() throws Exception {
ActionContext.getContext().getParameters();
if (bookFile == null) {
throw new Exception("上传文件出错!");
}
FileInputStream fis = null;
FileOutputStream os = null;
StringBuffer outPath = new StringBuffer();
outPath.append(phoenixProp.getProperty("book_file_folder"));
outPath.append(File.separator);
RBook book = iBookService.findBook(bookId);
if (book == null) {
throw new Exception("数据库中无法找到目标书籍!");
}
outPath.append(book.getBookId());
outPath.append(File.separator);
outPath.append(bookFileFileName);
File file = new File(outPath.toString());
try {
if (file.exists()) {
file.delete();
}
fis = new FileInputStream(bookFile);
os = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 16];
try {
while ((fis.read(buffer)) != -1) {
try {
os.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = (RequestMap) request;
}
}
<form id="uploadBookFrm" enctype="multipart/form-data" method="POST" action="/phoenixCloud/book/uploadBook.do">
<span class="btn btn-default btn-file">
<span class="fileinput-new">选择书籍文件</span>
<span class="fileinput-exists">重新选择书籍文件</span>
<input id="bookFile" type="file" name="bookFile">
</span>
<span class="fileinput-filename">snap.sql</span>
<a class="close fileinput-exists" style="float: none" data-dismiss="fileinput" href="#">×</a>
<input type="hidden" value="1" name="bookId">
<input type="submit" value="submit" name="submit">
</form>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.multipart.maxSize" value="104857600" />
...
<action name="uploadBook" class="bookUploadAction" method="uploadBook">
<interceptor-ref name="fileUpload">
<param name="maximumSize">104857600</param>
</interceptor-ref>
<result name="success" type="redirect">/book/bookDire_getAll.do?bookId=${bookId}</result>
</action>