一开始是做 获取表单请求的试验 但是我发现我一用struts,读取的内容就读不出来,如果仅仅是request.getParameter("XX")还可以读取到参数,但是getInputStream()就读不出来里面的信息,页面上显示的也是白板,部署时没问题,也没抛异常。但是如果不用struts的话,就一切正常。一开始我还以为是没导那些上传文件用的包的原因,后来发现其实是有的。请问各位大大到底什么原因?
------解决方案--------------------
好久没有 sf
------解决方案--------------------
用getInputStream()之前,试下用clear()清空下流
------解决方案--------------------
按你的意思,你是要获取表单上传过来的文件
先不说流怎么获取,我感觉你把struts1的方式用在struts2上了
我说下思路,不知道能不能帮你解决,我是先学用struts2,然后才学用struts1的
表单type="file" name="upload"
struts2获取jsp传过来的数据,不需要request.get。。。这个方法。从action传到jsp页面也是不用request.set。。。这个方法,都是对应着javabean 在action方法中做set get 的方法就可以获取到数据或传出数据
要想获取file这个文件,在对应的action方法中
定义3个private file upload ,String uploadFileName;String uploadContentType 后面2个是必须这么写的,set get 一下
然后就可以获取file upload 整个文件了
附加点代码比较明了
public class GiftAction {
private static final int BUFFER_SIZE = 16 * 1024;
private IntfInfoService intfService;
private List<GiftInfo>giftinfolist;
private GiftInfo giftInfo;
private int currentPage = 1;// 当前页
private int rsCount=0; //总记录数
private int giftid;
private String savePath;//常量,由struts.xml中的action节点的param子节点获得
private File upload; // 上传文件域对象
private String uploadFileName; // 上传文件名
private String uploadContentType; // 上传文件类型
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String addGiftinfo(){//添加礼品
String realpath=ServletActionContext.getServletContext().getRealPath(savePath);
String newFileName=getnewFileName(uploadFileName);
File target=new File(realpath+"/"+newFileName);
try {
copy(upload, target);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
giftInfo.setImage(newFileName);
intfService.addgift(giftInfo);
try {
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out;
out = response.getWriter();
String message = new String("填加成功!是否继续添加?".getBytes(), "ISO-8859-1");
out.print("<script>if(window.confirm('"+message+"')){window.history.back();}else{location.href='INTF/GiftAction!selectGiftinfo'}</script>");
out.flush();
out.close();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String getnewFileName(String uploadFileName) {//编辑上传文件的名字
String name=System.currentTimeMillis+uploadFileName.substring (uploadFileName.lastIndexOf(".",
uploadFileName.length()));
return name;
}
public void copy(File src, File target) throws Exception {//上传到文件夹中
InputStream in = new BufferedInputStream(new FileInputStream(src));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(target));
int len = 0;