当前位置:首页 » 文件管理 » action文件上传

action文件上传

发布时间: 2022-07-30 04:55:15

Ⅰ 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属性怎么设置

  1. action就写url啊,跟视图没直接关系,你要指定某个视图的话,可以用{% url %}标签;不过常见的做法action就是空,也就是当前视图,在视图里通过判断method是GET还是POST进行处理

  2. 没深究过,就是解决跨站访问啥的问题

Ⅷ 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 方法即可了

热点内容
线路需要配置哪些保护 发布:2025-04-13 11:29:32 浏览:218
靓密码现在有多少店 发布:2025-04-13 11:22:18 浏览:319
ccf编译出错 发布:2025-04-13 11:20:03 浏览:200
神雕侠侣古天乐版缓存 发布:2025-04-13 11:19:02 浏览:715
安卓手机后面的标志怎么画 发布:2025-04-13 11:17:37 浏览:801
jdk的编译命令是 发布:2025-04-13 11:17:37 浏览:533
黄金算法 发布:2025-04-13 10:54:56 浏览:994
安卓开发如何修改压缩包内文件 发布:2025-04-13 10:48:25 浏览:154
引导式访问密码忘了 发布:2025-04-13 10:34:09 浏览:24
大金仓数据库 发布:2025-04-13 10:14:38 浏览:811