请问,strus 如何实现上传文件、图片

Delphicaijie 2010-09-17 10:32:32

郁闷哪,无从下手!
...全文
92 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
eemean 2010-09-17
FormBean:

public class PersoForm extends ActionForm {
private FormFile pic;
public FormFile getPic() {
return pic;
}
public void setPic(FormFile pic) {
this.pic = pic;
}
...

Action:

public ActionForward persis(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws SQLException {
Integer id=0;
if(!request.getParameter("id").equals(""))id=new Integer(request.getParameter("id"));
boolean self=false;
Perso psSess=(Perso)request.getSession().getAttribute("u");
if(psSess.getId().equals(id))self=true;
SimpleDateFormat df=new SimpleDateFormat("MMddHHmmss");
String datStr=df.format(new Date());
PersoForm uf=(PersoForm)form;
uf.setPsw(null);
//Ou.pl(uf.getPsw());
Perso ps=new Perso();
BeanUtils.copyProperties(uf,ps);

if(self){
ps.setJobid(new Integer(request.getParameter("job2")));
ps.setGroupid(new Integer(request.getParameter("gru2")));
}
if(ps.getGroupid()>4)ps.setJobid(uf.getJobid());
String dir="";
try {
if(uf.getPic().getFileName()==null||uf.getPic().getFileName().equals("")){
request.setAttribute("msg", "未选择文件上传");
}else{
String uploadPath = request.getSession().getServletContext().getRealPath("/");
dir="\\upload\\sig"+datStr+"."+(uf.getPic().getFileName().split("\\.")[1]) ;
String dir1=uploadPath+dir;
OutputStream out=new FileOutputStream(new File(dir1));
System.out.println(uf.getPic());
byte b[]=uf.getPic().getFileData();
out.write(b);
out.close();
ps.setPhoto("/upload/sig"+datStr+"."+uf.getPic().getFileName().split("\\.")[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
persoBiz.persis(ps);
}catch(SQLException e){
if(e.getErrorCode()==1062){
request.setAttribute("msg", "已存在");
return mapping.findForward("mm");
}
}
request.setAttribute("msg", "已保存"+ps.getName());
if(self)
return new ActionForward("/perso.do?op=pre&id="+id);
else
return mapping.findForward("lis");
}

回复
jack_liu4Ye 2010-09-17
上传文件用到commons-fileupload-1.2.1.jar



FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List list = upload.parseRequest(request);
Iterator iter = list.iterator();
FileItem item = null;
String filePath = "";
String fileName = "";
String s = "";
String suffix = "";
long size = 0 ;
while (iter.hasNext()) {
item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = item.getName();
s = fileName.substring(fileName.lastIndexOf("\\") + 1);
suffix = s.substring(s.lastIndexOf("."));
s = uuid + suffix;
filePath = ConstantFactory.getConstant("path.upload.dcm").concat("\\\\").concat(s);
size = item.getSize();
if (size != 0) {
File uploadedFile = new File(filePath);
item.write(uploadedFile);
}
}
}
回复
RefreshingBreeze 2010-09-17
围观
回复
flybird 2010-09-17
<% @ taglib prefix = " st " uri = " /struts-tags " %>

<st:file name ="myFile" label ="Select File" />

后台的action中直接定义 File myFile = null;

取出来的就是上传的file。
回复
gouxiongyaya 2010-09-17
在网上找找相关的资料,弄一下就好了。
回复
Funnyskyf 2010-09-17
网上不是有插件吗?你可以看看风中叶的struts2视频教程,里面讲的很详细
回复
Delphicaijie 2010-09-17
首先,谢谢上边的朋友!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  照着做了,应该代码没有问题,但是,好像文件并没有上传到指定的服务器目录中,在应用程序的WebRoot目录下(application\WebRoot\userbase\document)。
当然,也无法打开(下载)此上传的文件之后,我是试着将文件直接拷贝到相应目录(application\WebRoot\userbase\document),但是还是不行!有大侠知道是怎么一回事吗?
对了,报的错误如下:
HTTP Status 404 - /application/USERBASE/document/%E5%BC%80%E5%8F%91%E9%9C%80%E6%B1%82%E5%86%85%E5%AE%B9.doc
type Status report

message /application/USERBASE/document/%E5%BC%80%E5%8F%91%E9%9C%80%E6%B1%82%E5%86%85%E5%AE%B9.doc

description The requested resource (/application/USERBASE/document/%E5%BC%80%E5%8F%91%E9%9C%80%E6%B1%82%E5%86%85%E5%AE%B9.doc) is not available.
要说明的是,程序可以运行的起来,其他模块也OK。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-09-17 10:32
社区公告
暂无公告