Input type="file" 要获取上传文件的内容 后台该怎么写?

jhj735412 2011-07-22 10:05:01
比如Jsp中有

<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<th>附件:</th>
<td id="uploadfile">
<input id="attachment" class="text" type="file" name="attachment" size="60"/>
</td>
</tr>
</table>
</form>


我后台怎么获取呢上传的文件呢?
是传统的request.getParameter("attachment")么?
这样好像不对?
这个表单中只需要上传一个文件!
谢谢大家给出答案 ,最好有代码. PS:我不想用组件
...全文
68326 44 打赏 收藏 转发到动态 举报
写回复
用AI写文章
44 条回复
切换为时间正序
请发表友善的回复…
发表回复
jim-single 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 43 楼 的回复:]

加个hidden ,当file onchange 给hidden赋值,action去hidden的值就可以
[/Quote]



++
皮皮虾向前冲 2011-08-09
  • 打赏
  • 举报
回复
加个hidden ,当file onchange 给hidden赋值,action去hidden的值就可以
雨天要吃饭 2011-08-08
  • 打赏
  • 举报
回复
为什么非得自己写呢。
acertang 2011-08-02
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 bill0605030109 的回复:]
Java code

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
i……
[/Quote]

++
dragonsky_w 2011-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yanyuegongzuoshi 的回复:]
在后台的action中定义
private File attachment;// 实际上传文件

private String uploadContentType; // 文件的内容类型

private String uploadFileName; // 上传文件名

给他们加上get,set方法,后台就能自动捕获到了
[/Quote]

楼上采用的是struts2框架,这个上传文件很是方便的……
cchs29 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 jfei2011 的回复:]

用字符流好呢还是字节流好呢
[/Quote]我感觉字节流好点
cchs29 2011-08-01
  • 打赏
  • 举报
回复
如果没有struts的话,那么就用流获取吧
feifeikub 2011-07-30
  • 打赏
  • 举报
回复
完全可以实现了,上面的代码整理一下,比较全面了。
Phoenix Slade 2011-07-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 y2009270076 的回复:]
引用 6 楼 jhj735412 的回复:

引用 3 楼 yanyuegongzuoshi 的回复:
在后台的action中定义
private File attachment;// 实际上传文件

private String attachmentContentType; // 文件的内容类型

private String attachmentFileName; // 上传文……
[/Quote]
同解
我是小飞 2011-07-30
  • 打赏
  • 举报
回复
用字符流好呢还是字节流好呢
xyong1325 2011-07-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jhj735412 的回复:]
引用 8 楼 y2009270076 的回复:

引用 6 楼 jhj735412 的回复:

引用 3 楼 yanyuegongzuoshi 的回复:
在后台的action中定义
private File attachment;// 实际上传文件

private String attachmentContentType; // 文件的内容类型

private String……
[/Quote]同意
magicluo 2011-07-30
  • 打赏
  • 举报
回复
随便一搜一大把吧

这点学习能力也没有?
ZZZ5512536 2011-07-30
  • 打赏
  • 举报
回复
说得也差不多了吧
  • 打赏
  • 举报
回复

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import form.UploadForm;

public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
UploadForm uploadForm = (UploadForm)form;
FormFile formFile = uploadForm.getFile();

String path = request.getRealPath("/files")+"\\" + formFile.getFileName();
FileOutputStream fos = new FileOutputStream(path);
InputStream fis = formFile.getInputStream();

int i = -1;
byte[] b = new byte[100];
while((i = fis.read(b, 0, 100)) != -1)
{
fos.write(b, 0, i);
}
fos.close();
fis.close();

System.out.println(uploadForm.getName());
System.out.println(path);

return mapping.findForward("success");
}

}



package form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm
{
FormFile file;
String name;
public String getName() {
return name;
}
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
public void setName(String name) {
this.name = name;
}
}




<form-beans>
<form-bean name="uploadForm" type="form.UploadForm"/>
</form-beans>
<action path="/uploadFile" name="uploadForm" type="action.UploadAction">
<forward name="success" path="/upload.jsp"/>
</action>



<%@ page language="java" pageEncoding="utf-8"%>
<html>
<body>
<form action="uploadFile.do" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="大大大"/>
<br/>
<input type="file" name="file"/>
<br/>
<input type="submit" value="submit"/>
<br/>
<a href="upload.jsp">upload.jsp</a>
<br/>
</form>
</body>
</html>
chang1984024 2011-07-29
  • 打赏
  • 举报
回复
jsp中的内容

<form action="uploadS.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择第一个文件:<input type="file" name="file1" /><br>

<input value="上传" type="submit" />
</form>
chang1984024 2011-07-29
  • 打赏
  • 举报
回复
public class UploadSingle extends ActionSupport{
//得到上传文件
private File file1;
private String title;
//得到上传文件类型 格式 xxxContentType xxx为jsp中File域的name值
private String file1ContentType;
//得到上传文件名 格式 xxxFileName xxx为jsp中File域的name值
private String file1FileName;
//接受依赖注入的属性
private String savePath="/upload";
public void copy(File file ,String url)
{
try {
FileOutputStream fos = new FileOutputStream(url);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public String execute() throws Exception {
//根据服务器的文件保存地址和原文件名创建目录文件全路径

System.out.println(this.title);
System.out.println(this.file1FileName);
File file =this.getFile1();
System.out.println(ServletActionContext.getRequest().getRealPath("/upload"));
String hz=file1FileName.substring(file1FileName.lastIndexOf("."), file1FileName.length());
System.out.println("hz==="+hz);
String filename=""+System.currentTimeMillis()+hz;
String url=ServletActionContext.getRequest().getRealPath(this.savePath)+"\\"+filename;
System.out.println(url);
copy(file,url);
//以服务器的文件保存地址和原文件名建立上传文件输出流
return "success";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String getFile1ContentType() {
return file1ContentType;
}
public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
}
public String getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}

}


zzz小菜鸟zzz 2011-07-28
  • 打赏
  • 举报
回复
提供思路:可以 把文件全部写到临时文件里面 然后再对该临时文件进行操作
zzz小菜鸟zzz 2011-07-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 y2009270076 的回复:]

楼主,请问一下,你所说的PS是什么意思?
[/Quote]备注的意思。
毛豆先生Max 2011-07-27
  • 打赏
  • 举报
回复
如果用struts2的话,1楼的答案就可以
jamespengo 2011-07-26
  • 打赏
  • 举报
回复
struts1的话就这样了
Hashtable files = form.getMultipartRequestHandler()
.getFileElements();
加载更多回复(23)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧