uploadify3.2.1+struts 使用及问题总结
2015-05-06 16:44
260 查看
uploadify3.2.1下载地址:http://www.uploadify.com/download/ 点击打开链接
uploadify3.2.1英文文档说明:http://www.uploadify.com/documentation/ 点击打开链接
uploadify3.2.1中文文档说明:/article/10731138.html 点击打开链接
uploadify在线中文文档:http://slabs.sinaapp.com/uploadifydoc/ 点击打开链接
前端HTML代码:
JS代码:
$(function() {
//初始化控件
$('#software').uploadify({
'uploader' : 'softwareAction!uploadSoftware',// 上传文件到服务器的地址
'cancelImg' : getRootPath() + '/js/uploadify/cancel.png',
'fileDataName' : 'software',
'fileObjName' : 'software',
'queueID' : 'fileQueue',
'queueSizeLimit' : 1,
'displayData' : 'percentage',
'method' : 'post',
'folder' : '/data',
'auto' : false,
'buttonText' : '选择文件...',
'onUploadSuccess' : function(file, data, response) {// 单个文件上传成功触发
// data就是action中返回来的数据
},
'onQueueComplete' : function() {// 所有文件上传完成
$('#uploadSoftwareDiv').hide();
mask.hide();
alert("文件上传成功!");
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传
});
function getRootPath() {
// 获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
// 获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
// 获取主机地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
// 获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
});
如果你要在上传的时候附加一些数据,就可以用$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传</span>
});
之后使用$('#software').uploadify('upload', '*');这样才真正开始上传
后台java处理代码
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class SoftwareAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware {
private static final long serialVersionUID = -4887717911648338263L;
private HttpServletRequest request;// request
private HttpServletResponse response;// response
private Map<String, Object> session;
private File software;
private String softwareFileName;//注意这里是softwareFileName,由File software决定
private String filePath = "D:\\upload\\";
public void uploadSoftware() throws IOException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("imp", "导入失败!");
PrintWriter pw = null;
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
try {
if (this.software != null) {
File f = this.getSoftware();
String fileName = UUID.randomUUID().toString();
FileInputStream inputStream = new FileInputStream(f);
String name = fileName + softwareFileName.substring(softwareFileName.lastIndexOf("."));
FileOutputStream outputStream = new FileOutputStream(filePath + name);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
jsonObject.put("imp", "导入成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
pw = response.getWriter();
pw.print(jsonObject);
}
public void downloadSoftware() {
String softwareId = request.getParameter("softwareId");
SoftwareAddress softwareAddress = softwareAddressService.getLatestSoftwareAddress(new Integer(softwareId));
if (null != softwareAddress) {
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
String filePath = this.filePath + softwareAddress.getAddress();
try {
File file = new File(filePath);
String ext = softwareAddress.getAddress().substring(softwareAddress.getAddress().lastIndexOf(".") + 1)
.toUpperCase();
Softwares softwares = softwareAddress.getSoftwares();
String fileName = softwares.getSoftwarename() + "_" + softwares.getSoftwaretype() + "_"
+ softwares.getVersion() + "." + ext;
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/plain");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes(), "ISO8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(buffer);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Resource
public void setSoftwareService(SoftwareService softwareService) {
this.softwareService = softwareService;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
this.response.setContentType("text/html;charset=UTF-8");
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public File getSoftware() {
return software;
}
public void setSoftware(File software) {
this.software = software;
}
public String getSoftwareFileName() {
return softwareFileName;
}
public void setSoftwareFileName(String softwareFileName) {
this.softwareFileName = softwareFileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
}
<input id='software' type='file' name='software' style="z-index: 101"> 和'fileDataName' : 'software', 'fileObjName' : 'software',与private File software;要保持input的id、fileData、fileObjName、File一致!!!
问题:上传小文件时没问题,但是上传稍大文件时uploadify报 IO Error。
原因:可能是tomcat限制了上传文件的大小。
解决方案:在struts.xml中配置 <constant name="struts.multipart.maxSize" value="2000000000" /> value可根据需要配置大小(单位:bit)
uploadify3.2.1英文文档说明:http://www.uploadify.com/documentation/ 点击打开链接
uploadify3.2.1中文文档说明:/article/10731138.html 点击打开链接
uploadify在线中文文档:http://slabs.sinaapp.com/uploadifydoc/ 点击打开链接
前端HTML代码:
<div id='uploadSoftwareDiv' class="upload_software hide"> <span style="color:#ff0000;"><input id='software' type='file' name='software' style="z-index: 101"></span> <span style="color:#ff0000;"><div id="fileQueue" style="z-index: 101"></div></span> <input id='uploadSoftware' class="uploadify-button" style="width: 120px; height: 32px; z-index: 101" type='button' value='上传' /> <input id='cancleSoftware' class="uploadify-button" style="width: 120px; height: 32px; z-index: 101" type='button' value='取消' /> </div>
JS代码:
$(function() {
//初始化控件
$('#software').uploadify({
'uploader' : 'softwareAction!uploadSoftware',// 上传文件到服务器的地址
'cancelImg' : getRootPath() + '/js/uploadify/cancel.png',
'fileDataName' : 'software',
'fileObjName' : 'software',
'queueID' : 'fileQueue',
'queueSizeLimit' : 1,
'displayData' : 'percentage',
'method' : 'post',
'folder' : '/data',
'auto' : false,
'buttonText' : '选择文件...',
'onUploadSuccess' : function(file, data, response) {// 单个文件上传成功触发
// data就是action中返回来的数据
},
'onQueueComplete' : function() {// 所有文件上传完成
$('#uploadSoftwareDiv').hide();
mask.hide();
alert("文件上传成功!");
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传
});
function getRootPath() {
// 获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
// 获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
// 获取主机地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
// 获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
});
如果你要在上传的时候附加一些数据,就可以用$("#uploadSoftware").on('click', function() {
$('#software').uploadify('settings', 'formData', {
'softwareId' : _this.id
});
$('#software').uploadify('upload', '*');//上传</span>
});
之后使用$('#software').uploadify('upload', '*');这样才真正开始上传
后台java处理代码
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class SoftwareAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware {
private static final long serialVersionUID = -4887717911648338263L;
private HttpServletRequest request;// request
private HttpServletResponse response;// response
private Map<String, Object> session;
private File software;
private String softwareFileName;//注意这里是softwareFileName,由File software决定
private String filePath = "D:\\upload\\";
public void uploadSoftware() throws IOException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("imp", "导入失败!");
PrintWriter pw = null;
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
try {
if (this.software != null) {
File f = this.getSoftware();
String fileName = UUID.randomUUID().toString();
FileInputStream inputStream = new FileInputStream(f);
String name = fileName + softwareFileName.substring(softwareFileName.lastIndexOf("."));
FileOutputStream outputStream = new FileOutputStream(filePath + name);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
jsonObject.put("imp", "导入成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
pw = response.getWriter();
pw.print(jsonObject);
}
public void downloadSoftware() {
String softwareId = request.getParameter("softwareId");
SoftwareAddress softwareAddress = softwareAddressService.getLatestSoftwareAddress(new Integer(softwareId));
if (null != softwareAddress) {
// String path =
// ServletActionContext.getServletContext().getRealPath("/upload");
String filePath = this.filePath + softwareAddress.getAddress();
try {
File file = new File(filePath);
String ext = softwareAddress.getAddress().substring(softwareAddress.getAddress().lastIndexOf(".") + 1)
.toUpperCase();
Softwares softwares = softwareAddress.getSoftwares();
String fileName = softwares.getSoftwarename() + "_" + softwares.getSoftwaretype() + "_"
+ softwares.getVersion() + "." + ext;
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/plain");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes(), "ISO8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(buffer);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Resource
public void setSoftwareService(SoftwareService softwareService) {
this.softwareService = softwareService;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
this.response.setContentType("text/html;charset=UTF-8");
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public File getSoftware() {
return software;
}
public void setSoftware(File software) {
this.software = software;
}
public String getSoftwareFileName() {
return softwareFileName;
}
public void setSoftwareFileName(String softwareFileName) {
this.softwareFileName = softwareFileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
}
<input id='software' type='file' name='software' style="z-index: 101"> 和'fileDataName' : 'software', 'fileObjName' : 'software',与private File software;要保持input的id、fileData、fileObjName、File一致!!!
问题:上传小文件时没问题,但是上传稍大文件时uploadify报 IO Error。
原因:可能是tomcat限制了上传文件的大小。
解决方案:在struts.xml中配置 <constant name="struts.multipart.maxSize" value="2000000000" /> value可根据需要配置大小(单位:bit)
相关文章推荐
- 在使用Struts的过程中也遇到了一些问题,现在总结一下
- 学习struts2建bbs总结五:使用jquery+ajax验证用户名是否存在以及struts效验信息不断重复的问题
- angularjs 使用uploadify出现的问题总结
- ssh整合问题总结--使用struts2+Ajax+jquery验证用户名是否已被注册
- 【postgresql】初次使用遇到的问题和总结
- Diffy使用过程问题总结
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- 使用Camera SurfaceView预览出现图案形变等问题解决总结
- sublime2使用过程中遇到的一些问题总结
- 关于使用MyEclipse自动生成Hibernate和Struts出现的jar不兼容的问题(antlr.collections.AST.getLine()I)
- ubuntu15.0使用问题总结
- Android ImageView 常见问题与使用总结
- 关于uploadify使用的一些小问题
- Spring使用——环境部署和配置问题总结
- ueditor使用中的问题总结
- [iOS]AFNetworking使用遇到的问题总结
- sqoop使用经验总结及问题汇总
- HP pavilion 安装ubuntu14.04后无线无法使用问题总结
- Linux下FreeTDS使用及问题总结
- 使用libsvm实现文本分类(问题总结)