導航:首頁 > 文件教程 > 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

友情鏈接