最近写上传文件,用到了这个东西,我想很多人对这个并不陌生。
下面贴出代码。
commons-fileupload 在struts1.x中的应用方法
DynaActionForm uf = (DynaActionForm) form;
FormFile file = (FormFile) uf.get("file");
System.out.println(file.getFileName());
String trackno=request.getParameter("trackno");
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String fileName= "temp"+myFmt.format(new Date()) +".xls";
String realPath ="";
realPath = uploadRootPath + File.separator + fileName;
FileOutputStream fout = null;
fout = new FileOutputStream(realPath);
fout.write(file.getFileData());
fout.flush();
fout.close();
在springMVC中
Iterator<FileItem> itr = fileItems.iterator();
Map<String, Object> map = new HashMap<String, Object>();
while (itr.hasNext()) {// 依次处理每个文件
FileItem item = (FileItem) itr.next();
String fileName = item.getName();// 获得文件名,包括路径
if(!item.isFormField()){
if (!"".equals(fileName)) {
File fullFile = new File(item.getName());
File savedFile = new File(savePath, fullFile.getName());
try {
item.write(savedFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
if("title".equals(item.getFieldName())){
title = new String(item.getString().getBytes("ISO-8859-1"),"UTF-8");
}
if("kinds".equals(item.getFieldName())){
String category1=new String(item.getString().getBytes("ISO-8859-1"),"UTF-8");
categoryList.add(category1);
}
if("content".equals(item.getFieldName())){
content=new String(item.getString().getBytes("ISO-8859-1"),"UTF-8");
}
}
}
}
如果单独使用的话,如果想获取表单中其他信息 需要判断,才可以
spring上传文件方式:
Iterator<String> it = multiRequest.getFileNames();
while(it.hasNext()){
String key=it.next();
MultipartFile file = multiRequest.getFile(key);
String filename=RandomStringUtils.randomNumeric(10)+file.getOriginalFilename();
//fileUploadHelper.SaveFileFromInputStream(file.getInputStream(), savePath, filename);
final File targetFile = new File(savePath + File.separator + fileName);
file.transferTo(targetFile);
此处是一个多文件上。单文件都可以
和所有新手一起分享下
分享到:
相关推荐
开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2...
赠送jar包:commons-fileupload-1.4.jar; 赠送原API文档:commons-fileupload-1.4-javadoc.jar; 赠送源代码:commons-fileupload-1.4-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.4.pom; 包含...
赠送jar包:commons-fileupload-1.3.3.jar; 赠送原API文档:commons-fileupload-1.3.3-javadoc.jar; 赠送源代码:commons-fileupload-1.3.3-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.3.3.pom;...
commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-...
commons-fileupload-1.3.3.jar和commons-io-2.6.jar最新版本
commons-fileupload-1.2.1.jar包;commons-lang-2.3.jar,在使用组件实现文件上传时候要注意前后台的一致性,注意在后台定义File文件对象名要与jsp页面中的file标签name名字一致,前台form标签属性一定要设置为...
commons commons-fileupload历史版本jar包集合,包括src源码 附件列表: commons-fileupload-1.0.zip commons-fileupload-1.1.1.zip commons-fileupload-1.1.zip commons-fileupload-1.2.1-bin.zip commons-...
commons-fileupload-1.3.2.jar和commons-io-2.5.jar
commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar jar 文件。 commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 案例上传: http://hi.baidu.com/lichao77821/blog commons-fileupload-1.2.1.jar和commons-...
commons-fileupload-1.3.jar和commons-io-1.2.jar
赠送jar包:commons-fileupload-1.3.3.jar; 赠送原API文档:commons-fileupload-1.3.3-javadoc.jar; 赠送源代码:commons-fileupload-1.3.3-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.3.3.pom;...
commons-fileupload-1.2.2-bin.zip一个文件上传的第三方插件
commons-fileupload-1.3.1.jar
commons-fileupload-1.4.jar
commons-fileupload-1.3.2jar包和commons-io-2.5jar包,可以到http://commons.apache.org里面下载最新版本。
明明只是几个jar包,其他资源真坑爹,可惜不能设为0资源分! commons-fileupload-1.3.3.jar commons-io-2.5.jar 利用这两个jia包完成资源上传功能
commons-fileupload ,commons-io commons-fileupload ,commons-io commons-fileupload ,commons-io
Apache的commons-fileupload.jar可方便的实现文件的上传功能
包含commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 下载即可使用
commons-fileupload.rar;包括commons-fileupload-1.3.1-bin和commons-io-2.4两个文件;文件上传接口需要这个包;