public void loadWord() { // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
File tmpDir = new File("d:\\temp"); // 初始化上传文件的临时存放目录,必须是绝对路径
try {
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 指定在内存中缓存数据大小,单位为byte,这里设为1Mb
factory.setSizeThreshold(1 * 1024 * 1024);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
factory.setRepository(tmpDir);
ServletFileUpload sfu = new ServletFileUpload(factory);
// 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb
sfu.setFileSizeMax(5 * 1024 * 1024);
// 指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb
sfu.setSizeMax(10 * 1024 * 1024);
sfu.setHeaderEncoding("UTF-8"); // 设置编码,因为我的jsp页面的编码是utf-8的
FileItemIterator fii = sfu.getItemIterator(request);// 解析request请求
uploadPath = uploadPath + "upload\\"; // 选定上传的目录此处为当前目录
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs(); // 选定上传的目录此处为当前目录,没有则创建
} int index = 0;
while (fii.hasNext()) {
FileItemStream fis = fii.next();// 从集合中获得一个文件流
if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
String fileName = fis.getName().substring(
fis.getName().lastIndexOf("."));// 获得上传文件的文件名
log.info(fileName);
// fileName = sdf.format(new Date())+"-"+index+fileName;
BufferedInputStream in = new BufferedInputStream(fis
.openStream());
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + "\\"
+ fileName)));
Streams.copy(in, out, true); // 开始把文件写到你指定的上传文件夹
index++;
}
} }
} catch (Exception e) {
e.printStackTrace();
}
}FileItemIterator fii = sfu.getItemIterator(request);/在这里为什么得不到值??????????? 我写在struts2中的
File tmpDir = new File("d:\\temp"); // 初始化上传文件的临时存放目录,必须是绝对路径
try {
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 指定在内存中缓存数据大小,单位为byte,这里设为1Mb
factory.setSizeThreshold(1 * 1024 * 1024);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
factory.setRepository(tmpDir);
ServletFileUpload sfu = new ServletFileUpload(factory);
// 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb
sfu.setFileSizeMax(5 * 1024 * 1024);
// 指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb
sfu.setSizeMax(10 * 1024 * 1024);
sfu.setHeaderEncoding("UTF-8"); // 设置编码,因为我的jsp页面的编码是utf-8的
FileItemIterator fii = sfu.getItemIterator(request);// 解析request请求
uploadPath = uploadPath + "upload\\"; // 选定上传的目录此处为当前目录
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs(); // 选定上传的目录此处为当前目录,没有则创建
} int index = 0;
while (fii.hasNext()) {
FileItemStream fis = fii.next();// 从集合中获得一个文件流
if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
String fileName = fis.getName().substring(
fis.getName().lastIndexOf("."));// 获得上传文件的文件名
log.info(fileName);
// fileName = sdf.format(new Date())+"-"+index+fileName;
BufferedInputStream in = new BufferedInputStream(fis
.openStream());
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(uploadPath + "\\"
+ fileName)));
Streams.copy(in, out, true); // 开始把文件写到你指定的上传文件夹
index++;
}
} }
} catch (Exception e) {
e.printStackTrace();
}
}FileItemIterator fii = sfu.getItemIterator(request);/在这里为什么得不到值??????????? 我写在struts2中的
解决方案 »
- 如何在Spring AOP的前置通知中获取代理的对象
- deleteAll()方法出现异常,Batch update returned unexpected row ……,并发的问题?
- dom4j解析XML报错:Error on line -1 of document : Premature end of file.
- 有个问题 大家来帮帮忙啊
- struts2没有自己独特的标签检验程序吗
- DataModel
- 应用Spring的Aop方法 怎样方便的代理多个类
- eclipse的高手能否写个step by step 给我们享受享受呀!
- 关于在RMI中使用激活对象时启动多个JVM的问题。谢谢:)
- 用JAVA通讯的方法
- 关于SSI框架 业务逻辑测试问题
- 高手帮我解决一下哈,java保存文件代码怎么木有反应。
<%@ page import="java.sql.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html>
<head>
<link href="css/templates/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/templates/main.js"></script>
<title>编辑正文</title>
<!-- --------------------=== 调用Weboffice初始化方法 ===--------------------- --> <SCRIPT LANGUAGE=javascript FOR=WebOffice1 EVENT=NotifyCtrlReady>
/****************************************************
*
* 在装载完Weboffice(执行<object>...</object>)
* 控件后执行 "WebOffice1_NotifyCtrlReady"方法
*
****************************************************/
WebOffice1_NotifyCtrlReady()
</SCRIPT>
<SCRIPT LANGUAGE=javascript FOR=WebOffice1 EVENT=NotifyWordEvent(eventname)>
<!--
WebOffice1_NotifyWordEvent(eventname)
//-->
</SCRIPT>
<SCRIPT language=javascript event=NotifyToolBarClick(iIndex) for=WebOffice1>
<!--
WebOffice1_NotifyToolBarClick(iIndex)
//-->
</SCRIPT> <SCRIPT LANGUAGE=javascript>
/****************************************************
* 控件初始化WebOffice方法
****************************************************/
function WebOffice1_NotifyCtrlReady() {
document.all.WebOffice1.OptionFlag |= 128;
//document.all.WebOffice1.ShowToolBar=0, //显示/隐藏菜单
var ids=document.getElementById("ididid").value; if(ids=="0" || ids=="")
{
document.all.WebOffice1.LoadOriginalFile("", "doc");
}else if(ids!="")
{
document.all.WebOffice1.LoadOriginalFile("/getWordContent.action?id="+ids, "doc");
}
document.all.WebOffice1.HideMenuItem(0x01+0x02+0x04);
//document.all.WebOffice1.SetCustomToolBtn(0, "暂存本地")
//document.all.WebOffice1.HideMenuAction(1,1048576+2097152+4194304);
//document.all.WebOffice1.HideMenuAction(5,0);
}function WebOffice1_NotifyToolBarClick(iIndex){
if(iIndex>=32776){
document.all.WebOffice1.SaveAs("C:\Users\yx\test.doc",0);
}}
var flag=false;
function menuOnClick(id){
var id=document.getElementById(id);
var dis=id.style.display;
if(dis!="none"){
id.style.display="none";
}else{
id.style.display="block";
}
}
/****************************************************
*
* 接收office事件处理方法
*
****************************************************/
var vNoCopy = 0;
var vNoPrint = 0;
var vNoSave = 0;
var vClose=0;
function no_copy(){
vNoCopy = 1;
}
function yes_copy(){
vNoCopy = 0;
}
function no_print(){
vNoPrint = 1;
}
function yes_print(){
vNoPrint = 0;
}
function no_save(){
vNoSave = 1;
}
function yes_save(){
vNoSave = 0;
}
function EnableClose(flag){
vClose=flag;
}
function CloseWord(){
document.all.WebOffice1.CloseDoc(0);
}function WebOffice1_NotifyWordEvent(eventname) {
if(eventname=="DocumentBeforeSave"){
if(vNoSave){
document.all.WebOffice1.lContinue = 0;
alert("此文档已经禁止保存");
}else{
document.all.WebOffice1.lContinue = 1;
}
}else if(eventname=="DocumentBeforePrint"){
if(vNoPrint){
document.all.WebOffice1.lContinue = 0;
alert("此文档已经禁止打印");
}else{
document.all.WebOffice1.lContinue = 1;
}
}else if(eventname=="WindowSelectionChange"){
if(vNoCopy){
document.all.WebOffice1.lContinue = 0;
//alert("此文档已经禁止复制");
}else{
document.all.WebOffice1.lContinue = 1;
}
}else if(eventname =="DocumentBeforeClose"){
if(vClose==0){
document.all.WebOffice1.lContinue=0;
} else{
document.all.WebOffice1.lContinue = 1;
}
}
} //alert(eventname);
/****************************************************
*
* ---保存本地
*
/**/</SCRIPT>
<link rel="stylesheet" type="text/css" href="css/templates/style.css" />
</head>
<body onbeforeunload="return window_onbeforeunload()">
<center>
<div align="center"
> <form name="myform" ENCTYPE="multipart/form-data" METHOD="POST" action="/project/uploadWord.action">
<table width="90%" BORDER="1" bordercolor="#CCCCCC" CELLSPACING="0" CELLPADDING="3" style="border-collapse:collapse" >
<tr class="TableHeader">
<td colspan="4">
文档操作<input type="file" name="aaaaa">
<input type="hidden" id="ididid" value="${id }">
</td>
</tr>
<tr>
<td class="TableData">
项目名称:
<input name="DocTitle" value="${proname }"size="14">
</td>
<td class="TableData">项目编号:
<input name="DocID" value="${proid }" size="14"></td>
<!-- <td class="TableData"><input name="button93" type="button"-->
<!-- onClick="return SaveDoc('${id }','doc')" value="提交"-->
<!-- classs="rollout"></td> -->
<input type="submit" value="tijiao"/>
</tr>
<tr>
<td width="85%" valign="top" colspan="3"> <!-- -----------------------------== 装载weboffice控件 ==--------------------------------- -->
<script src="js/templates/LoadWebOffice.js"></script>
<!-- --------------------------------== 结束装载控件 ==----------------------------------- -->
</td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>设置了
Iterator it = fileItems.iterator();
再试试
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> itr = items.iterator();
private File aaaaa;
private String aaaaaFileName;
private String aaaaaContentType;get set......你就可以像操作new出来的File一个操作这个aaaaa的File对象