Struts2的上传

来源:互联网 发布:少女前线索米数据 编辑:程序博客网 时间:2024/05/20 06:07

jsp页面代码:

                                                <div class="upload"><form action="/wemiss/uploadpic.action" method="post" enctype="multipart/form-data" class="picuploadform"><input type="button" class="uploadpic" value="点击上传头像"/><input type="file" class="picfile" name="resourcefile"/><span class="infoshow">照片大小仅支持150*140</span></form></div>

java后台代码:

public String uploadpic(){    String path = session.getServletContext().getRealPath("/uploadfile/");//上传的服务器上的path   try {FileInputStream fis = new FileInputStream(resourcefile);//通过临时文件.tmp来拿到文件的输入流    int ishave=0;byte[] b=new byte[1024];File outfile=new File(path);if(!outfile.isDirectory()){outfile.mkdir();}FileOutputStream fos = new FileOutputStream(new File(path,resourcefileFileName));while((ishave=fis.read(b))!=-1){fos.write(b);}fos.close();System.out.println("上传成功");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}  
/*  在后台代码中,定义三个变量,并给出set方法,struts会自动往属性里注入值 
1. private File resoursefile(和你在jsp页面input file标签里的name属性值相同)---->临时文件
2.private String  <span style="font-family: Arial, Helvetica, sans-serif;">resourcefileFileName     ------>上传的文件名</span>
<span style="font-family:Arial, Helvetica, sans-serif;">3.private String  </span><span style="font-family: Arial, Helvetica, sans-serif;">resourcefileContentType    ------->上传文件的类型</span><span style="font-family:Arial, Helvetica, sans-serif;"></span>*/                                                                                                                                                       

0 0