uploadify3.2.1+struts 使用及问题总结

7 篇文章 0 订阅
4 篇文章 0 订阅

uploadify3.2.1下载地址:http://www.uploadify.com/download/  点击打开链接

uploadify3.2.1英文文档说明:http://www.uploadify.com/documentation/  点击打开链接

uploadify3.2.1中文文档说明:http://blog.csdn.net/lithinkingboy/article/details/45539371  点击打开链接

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)

 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uploadify 2.1.0 是一种用于在 Spring MVC 中实现文件上传的插件。 Spring MVC 是一个基于 Java 的 MVC(模型-视图-控制器)框架,用于构建 Web 应用程序。它提供了一种优雅的方式来处理客户端请求和响应,并支持各种功能,如路由、表单处理和文件上传。 uploadify 2.1.0 是一种用于在前端页面上实现文件上传的 JavaScript 插件。它具有易用性和灵活性的特点,可以与 Spring MVC 集成,实现文件上传功能。 要在 Spring MVC 中使用 uploadify 2.1.0,首先需要在前端页面引入 uploadify 的 JavaScript 文件,并创建一个上传按钮和一个用于显示上传进度的容器。 然后,在后端的 Spring MVC 控制器中,创建一个处理文件上传的方法。可以使用 Spring 的 MultipartFile 类型来接收上传的文件。在处理方法中,可以利用 MultipartFile 类提供的方法来获取文件的相关信息,如文件名、大小和内容。 处理方法可以进行一些上传文件的逻辑,如检查文件类型、大小或保存文件到服务器的指定路径。可以利用 Spring MVC 的文件上传功能,来处理上传的文件,并返回相应的响应结果给前端页面。 最后,可以在页面上通过监听 uploadify 的一些回调事件,来处理上传完成后的操作,如显示上传成功或失败的消息,或刷新页面以显示最新的上传文件列表。 总结来说,uploadify 2.1.0 和 Spring MVC 可以很好地配合使用,实现在 Web 应用程序中的文件上传功能,提升用户体验和数据操作的便利性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值