导航:首页 > 文件教程 > extstruts2文件上传

extstruts2文件上传

发布时间:2021-03-07 10:45:23

A. ext3.0+struts2.0 在上传图片成功后,把图片保存在项目中,如果要修改时,怎么把上传后的图片显示出来

修改的时候会从数据库中查出这张图片的url+img名称啊,你指定url不就行了?

B. struts2+extjs上传excel文件的问题

给你extjs能执行的你研究一下
我觉得你设置 xtype: 'filefield', 能好用
*************************************************************************
*上传框组件
*
************************************************************************
*/
Ext.define('Mocoolka.web.coreview.container.MKUploadForm', {
extend:'Ext.form.Panel',
frame: true,
autoScroll: true,
initComponent: function () {
var me = this;

me.title = getUIWithID("SystemUI.Buttons.Upload.Description");//'上传',

me.items = [
{
xtype: 'filefield',
emptyText: getUIWithID("SystemUI.MKUploadForm.SelectFile.Description"),//'选择一个文件',
name: 'filename',
buttonText: '...',
buttonConfig: {
iconCls: 'upload-icon'
}
},
];

me.buttons = [{
text: getUIWithID("SystemUI.Buttons.Upload.Description"),//'上传',
handler: function () {
var form = this.up('form').getForm();

var action = this.up('form').mkaction;
var myaction = "import";
if (action.get("Name") == "ImportAttachment")
myaction = "ImportAttachment";
var url = mkruntimer.getDataManager().getUrlPath(myaction, action);

if (form.isValid()) {
form.submit({
url: url,
waitMsg: getUIWithID("SystemUI.Buttons.Uploading.Description"),//'上传中...',
success: function (fp, o) {

var form1 = form.owner;
form1.mkcallout(form1.mkcalloutpara, action.result.children);
form1.up('window').close();

},
failure: function (form, action) {
mkerrorutil.processAjaxFailure(action.response);

}
});
}
}
}, {
text: getUIWithID("SystemUI.Buttons.Reset.Description"),//'重设',
handler: function () {
this.up('form').getForm().reset();
}
}, {
text: getUIWithID("SystemUI.Buttons.Cancel.Description"),//'取消',
handler: function () {
this.up('window').close();
}
},
{
text: getUIWithID("SystemUI.MKUploadForm.AddFile.Description"),//'增加一个文件',
handler: function () {
this.up('form').addFile();

}
}
]
me.callParent(arguments);

},
addFile: function () {
var me = this;
me.add({
xtype: 'filefield',
emptyText: getUIWithID("SystemUI.MKUploadForm.SelectFile.Description"),//'选择一个文件',
fieldLabel: getUIWithID(""),// '文件',
name: 'filename',
buttonText: '',
buttonConfig: {
iconCls: 'upload-icon'
}
});

},
//standardSubmit:false,

bodyPadding: '10 10 0',
flex:1,
defaults: {
anchor: '100%',

msgTarget: 'side',
labelWidth: 50
},
region: 'center',

});

C. struts2实现图片的上传和下载

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import com.zdvictory.taurus.common.util.*;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;

/** *
*/
public class UploadFileHandler {

private static int BUFFER_SIZE = 8192;

/**
* 上传附件操作 传递参数:系统参数配置设置的参数名称
*/
@SuppressWarnings("unchecked")
public static List<Attachment> upload(String sysParaName) throws Exception {
// 文件保存路径
String path = SysParaFinder.getSysParaValue(sysParaName);
List<Attachment> list = new ArrayList<Attachment>();
MultiPartRequestWrapper request = (MultiPartRequestWrapper) ServletActionContext
.getRequest();
Enumeration enu = request.getFileParameterNames();
while (enu.hasMoreElements()) { // 对每一个文件域进行遍历
String controlName = (String) enu.nextElement();
String[] fileNames = request.getFileNames(controlName);
File[] uploadFiles = request.getFiles(controlName);
for (int i = 0; i < uploadFiles.length; i++) {
File uploadFile = uploadFiles[i];
if (!uploadFile.exists())
continue;
// 如果文件夹不存在,创建文件夹,将文件保存到目录
File dir = new File(request.getRealPath("/") + path);
if (!dir.exists())
dir.mkdirs();
String ext = fileNames[i].substring(fileNames[i].indexOf("."),
fileNames[i].length());// 获取文件扩展名
String filename = UUID.randomUUID().toString() + ext;
File file = new File(request.getRealPath("/") + path + filename);
byte[] data = new byte[BUFFER_SIZE];
int byteRead = -1;
FileInputStream in = new FileInputStream(uploadFile);
FileOutputStream out = new FileOutputStream(file);
while ((byteRead = in.read(data)) != -1) {
out.write(data, 0, byteRead);
out.flush();
}
out.close();
in.close();
// 设置附件对象属性
Attachment attach = new Attachment();
attach.setFilename(fileNames[i]);
attach.setContentType(ext);
attach.setFilepathname(path + filename);
attach.setFilesize(uploadFile.length());
list.add(attach);
}
}
return list;
}
}
文件下载

public String download() throws Exception {
redheadTemplate = redheadTemplateManager.findById(Long
.valueOf(getId()[0]));
String name = redheadTemplate.getName()
+ redheadTemplate.getFilepathname().substring(
redheadTemplate.getFilepathname().lastIndexOf("."),
redheadTemplate.getFilepathname().length());
this.setFilename(new String(name.getBytes(), "ISO8859-1"));
this.setFilepathname(redheadTemplate.getFilepathname());
return "download";
}
文件下载配置文件

<result name="download" type="stream">
<!-- 下载文件类型 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${filename}, 它将通过 Action 的 getFilename() 获得文件名 -->
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<!-- 下载的InputStream流,Struts2自动对应Action中的getDownloadFile方法,该方法必须返回InputStream类型 -->
<param name="inputName">downloadFile</param>
<!-- 输出时缓冲区的大小 -->
<param name="bufferSize">8192</param>
</result>

D. Struts2 传输数据到Ext

后台:request.setAttribute("LIST",your_list);
前台:$.ajax({
cache:false,
type:"POST",
url:"你后台action的路径",
success:function(data){//data用来接收你的list,此时接收过来的数据格式并不是版list需转化
var x=eval("("+data+")");//转换data格式,权此时的x 就是list了
接下来遍历,你想怎么处理就怎么处理了.....
}
});

E. struts2中在action中怎么对文件上传进行处理

InputStream is = null;
OutputStream ops = null;
for (int i = 0; i < file.size(); i++) {
try {
TFile tfile = new TFile();
if (file.get(i) == null) {
continue;
}
is = new FileInputStream(file.get(i));
//路径
String root = ServletActionContext.getRequest()
.getRealPath("/uploads");
// 根据系统时间取名字
int pos = this.getFileFileName().get(i).lastIndexOf(".");
String ext = this.getFileFileName().get(i).substring(pos);
// 获得系统时间,产生文件名
long now = System.currentTimeMillis();
String nowtime = String.valueOf(now);
File destFile = new File(root + "/", nowtime + ext);
ops = new FileOutputStream(destFile);
byte[] b = new byte[400];
int length = 0;
while ((length = is.read(b)) > 0) {
ops.write(b, 0, length);
// ops.write(b); 这样子同样可行
}
tfile.setFileName(getFileFileName().get(i));
tfile.setFilePath("/uploads/" + nowtime + ext);
tfile.setFileCreatetime(currentDate);
tfile.setFileDataid(uptdt.getDataId());
fileservice.saveFile(tfile);// 保存上传文件信息
} catch (Exception ex) {
ex.printStackTrace();

} finally {
is.close();
ops.close();
}
}

F. struts2 上传文件怎么修改上传文件的名字

SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssS");//格式化时间输出 String Rname=sdf.format(new Date());//取得当前时间,Date()是java.util包里的,这作为真实名称 String name=file.getFileName();//得到上传文件的原回名称
int i=name.lastIndexOf(".");//原名称里答倒数第一个"."在哪里
String ext=name.substring(i+1);//取得后缀,及"."后面的字符
name=Rname+"."+ext;//拼凑而成
file为你上传的文件~!

G. ExtJs中的文件上传下载功能求教,100分

http://hi..com/%B1%F9%C3%CE%CE%DE%BA%DB/blog/item/3b19542954c90ff799250a29.html

前台EXT(假设上传文件为2个):主要就是个formPanel,items中写为:

{
xtype : 'textfield',
fieldLabel : '上传文件1',
name : 'file',
inputType : 'file'
}, {
xtype : 'textfield',
fieldLabel : '上传文件2',
name : 'file',
inputType : 'file'
}

其他地方不详细诉说了,不明白看EXT表单提交去,别忘记fileUpload : true就可以了

注意:因为是用struts2处理,name都是一样的file

struts2后台:

private List<File> file;对应前面的name
// 使用列表保存多个上传文件的文件名
private List<String> fileFileName;
// 使用列表保存多个上传文件的MIME类型
private List<String> fileContentType;
// 保存上传文件的目录,相对于Web应用程序的根路径,在struts.xml文件中配置
private String uploadDir;

get/set方法略

public void fileUpLoad() {
String newFileName = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
String reInfo="";//上传成功返回的东西
for (int i = 0; i < file.size(); i++) {
long now = new Date().getTime();
int index = fileFileName.get(i).lastIndexOf('.');
String path = ServletActionContext.getServletContext().getRealPath(
uploadDir);
File dir = new File(path);
if (!dir.exists())
dir.mkdir();//创建个文件夹
if (index != -1)
newFileName = fileFileName.get(i).substring(0, index) + "-"
+ now + fileFileName.get(i).substring(index);//生成新文件名
else
newFileName = fileFileName.get(i) + "-" + now;
reInfo+=newFileName+"@";
bos = null;
bis = null;
try {
FileInputStream fis = new FileInputStream(file.get(i)); // /////////
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(new File(dir,
newFileName));
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (Exception e) {
/////错误返回
try {
HttpServletResponse response = ApplicationWebRequestContext
.getWebApplicationContext().getResponse();
String msg = "{success:false,errors:{name:'上传错误'}}";
response.getWriter().write(msg);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
////////
} finally {
////////////////////////善后
try {
if (null != bis)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
if (null != bos)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
////////////////////////////////
}

//最后若没错误返回就这里返回了
try {
HttpServletResponse response = ApplicationWebRequestContext
.getWebApplicationContext().getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String msg = "{success:true,msg:'"+reInfo+"'}";
response.getWriter().write(msg);

} catch (Exception e) {
e.printStackTrace();
}
}
改方法参考了孙鑫的strut2深入详解,自己修改而成

注意上面2个黑体,尤其是第2行,不加的话浏览器会很悲剧的再你上传陈宫以后的返回前后加上<pre>,于是ext表单获得返回失败,这就是为什么有些人用其他EXT上传组件时候上传成功一直卡成进度条那

阅读全文

与extstruts2文件上传相关的资料

热点内容
网络中常用的传输介质 浏览:518
文件如何使用 浏览:322
同步推密码找回 浏览:865
乐高怎么才能用电脑编程序 浏览:65
本机qq文件为什么找不到 浏览:264
安卓qq空间免升级 浏览:490
linux如何删除模块驱动程序 浏览:193
at89c51c程序 浏览:329
怎么创建word大纲文件 浏览:622
袅袅朗诵文件生成器 浏览:626
1054件文件是多少gb 浏览:371
高州禁养区内能养猪多少头的文件 浏览:927
win8ico文件 浏览:949
仁和数控怎么编程 浏览:381
项目文件夹图片 浏览:87
怎么在东芝电视安装app 浏览:954
plc显示数字怎么编程 浏览:439
如何辨别假网站 浏览:711
宽带用别人的账号密码 浏览:556
新app如何占有市场 浏览:42

友情链接