读书人

struts下传文件注意事项

发布时间: 2012-09-23 10:28:11 作者: rapoo

struts上传文件注意事项
1.导入两个包:commons-fileupload-1.1.1.jar和commons-io-1.1.jar
2.ActionForm类要有FormFile类型的属性
3.JSP页面的form需要有enctype="multipart/form-data"属性,并且必需是strut标签
4.Action类里获取服务器地址例子如下:

DocAddForm d_form = (DocAddForm) form; if (d_form != null) {    FormFile file = (FormFile) d_form.getFile();    try {       byte[] content1 = file.getFileData();       String path = request.getSession().getServletContext().getRealPath("");// 获取web服务器地址        File file1 = new File(path + "/files/" + file.getFileName());       FileOutputStream out = new FileOutputStream(file1);       out.write(content1);       out.close();    } catch (Exception e) {       e.getStackTrace();    } }

读书人网 >软件架构设计

热点推荐