action文件上傳
Ⅰ struts2 action 多 多 文件上傳的相關疑問 有能力者進
這個問題其實很簡單 首先你走的struts2 action和struts2.xml的配置就不說了 ,你可以去網上一搜一大把! 首先action層接受的是一個fileName。你對象下所對應的文件個數是不確定的,所以你得用 fileName[] 數組的形式,至於你所說的不知道用哪個對象對應哪個文件,這個其實也很好處理 ,首先jsp裡面接受的是一個name屬性,你後台配置的話只需要配置成name相同的file類型的屬性 比如你name為 pic 那麼你的屬性名為 picFileName dis 就為 disFileName 剩下的你只需要遍歷 picFileName[]這個數組... 如果還不明白 ,直接問
Ⅱ 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();
}
}
Ⅲ 編寫一個文件上傳的action程序,無需寫jsp界面和配置路徑
最簡單的jsp+servlet上傳,
無非都是在你的action代碼中用流將文件讀取過來,
然後用流將上傳的文件輸出到你想要保存的路徑。
Ⅳ 如何調用action實現上傳功能
可以把路徑傳到後台,後台用java代碼獲取文件不就行了。
Ⅳ jsp頁面上傳文件,走action的時候如何獲得上傳的文件完整路徑
到action怎麼會有文件路徑,到action的時候是文件流,跟著你把文件流寫入你new出來的文件對象里想放那就那
Ⅵ 文件上傳插件這個是什麼意思action="server/php
創建一個文件上傳表單
允許用戶從表單上傳文件是非常有用的。
請看下面這個供上傳文件的 HTML 表單:
1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
請留意如下有關此表單的信息:
<form> 標簽的 enctype 屬性規定了在提交表單時要使用哪種內容類型。在表單需要二進制數據時,比如文件內容,請使用 "multipart/form-data"。
<input> 標簽的 type="file" 屬性規定了應該把輸入作為文件來處理。
Ⅶ Django上傳文件 前端頁面form標簽里action屬性怎麼設置
action就寫url啊,跟視圖沒直接關系,你要指定某個視圖的話,可以用{% url %}標簽;不過常見的做法action就是空,也就是當前視圖,在視圖里通過判斷method是GET還是POST進行處理
沒深究過,就是解決跨站訪問啥的問題
Ⅷ JAVA action中如何 上傳 下載文件
/**
上傳文件
*/
public class FileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
FileForm fileform = (FileForm) form;
//取得請求的文件集合
Hashtable hash = fileform.getMultipartRequestHandler().getFileElements();
//得到hashtable的枚舉值
Enumeration enu = hash.elements();
//如果該枚舉值包含有其它的文件
while(enu.hasMoreElements()) {
//得到文件
FormFile file = (FormFile) enu.nextElement();
System.out.println(file);
add(file);
}
return mapping.findForward("yes");
} catch (Exception e) {
e.printStackTrace();
}
return super.execute(mapping, form, request, response);
}
public void add(FormFile file){
try {
//取得寫文件的目錄
String url=servlet.getServletContext().getRealPath("upload");
File f1=new File(url);
if(!f1.exists()){//如果文件目錄不存在
f1.mkdirs();//創建目錄
}
String fileName=file.getFileName();
//創建一個文件輸入流
InputStream is=file.getInputStream();
OutputStream out=new FileOutputStream(url+"/"+fileName);
int byteRead=0;
byte[] by=new byte[8192];
while((byteRead=is.read(by, 0, 8192))!=-1){
out.write(by, 0, byteRead);
}
out.close();
is.close();
file.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
下載文件
*/
頁面一開始進去action,action負責把file文件夾下的所有文件讀入一個ArrayList中
Action代碼如下:
ArrayList list = new ArrayList();
String path=request.getRealPath("/")+"file";
String FullPath;
//System.out.println(path);
myDir=new File(path);
list.clear();
contents=myDir.listFiles();
for(int i=0;i<contents.length;i++){
FullPath=contents.getName();
list.add(FullPath);
//System.out.println(FullPath);
}
request.setAttribute("list",list);
ActionForward forward=new ActionForward("/download.jsp");
return forward;
然後進入download.jsp中,這個頁面主要負責把所有文件顯示,並提供下載連接,代碼如下:
<%@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%>
<head>
</style>
</head>
<body>
<%ArrayList list=(ArrayList)request.getAttribute("list");
for(int i=0;i<list.size();i++)
{
String a=java.net.URLEncoder.encode((String)list.get(i));
out.print("<a href=./loaded.do?name="+a+">"+list.get(i)+"</a><br>");
}
%>
</body>
</html>
注意,下劃線畫中的代碼的作用,就是解決問題的所在。
接下來可以直接傳入到loadedaction中,也可以通過一個form,我演示的是通過一個form
Form代碼如下
package org.aeolus.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoadForm extends ActionForm {
/*
*Generated Methods
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接下來就是action的代碼
LoadForm doc=(LoadForm)form;
String docName = new String(doc.getName().getBytes("8859_1"));
File f;
if(docName!=""){
String docFullPath=request.getRealPath("/");
f = new File(docFullPath+"file\\"+docName);
response.reset();
response.setContentType("application/x-msdownload;charset=GBK");
System.out.print(response.getContentType());
response.setCharacterEncoding("UTF-8");
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
out.close();
response.wait();
ActionForward forward=new ActionForward("/download.jsp");
return forward; }
return null;
注意,下劃線畫中的代碼的作用,就是解決問題的所在。說明一下:
response.setCharacterEncoding("UTF-8");
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
如果不這樣做你將要下載的文件名是亂碼。
Ⅸ 求解釋這段javaweb中上傳文件action中的代碼,表示新手看不懂。。。最好每一條來個注釋
這個方法是重寫繼承自超類的execute()方法!字面上就能看出來這個方法的作用!看上去還是挺復雜的!看半天也似懂非懂!
Ⅹ struts2上傳文件的action怎麼寫
把各個屬性在一個實體類里聲明並有get,set方法,在action里聲明次實體類並有get set 方法即可了