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

Delphicaijie 2010-09-17 10:32:32

郁闷哪,无从下手!
...全文
127 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
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。

81,122

社区成员

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

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