首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

文件上传javaBean这么写?该如何解决

2012-01-16 
文件上传javaBean这么写?问题一:formaction uploadimage.jsp method post enctype multipart/for

文件上传javaBean这么写?
问题一:
<form   action= "uploadimage.jsp "   method= "post "   enctype= "multipart/form-data "   name= "form1 ">
<input   type= "file "   name= "file ">
<input   type= "submit "   name= "Submit "   value= "上传 "   >
</form>

在javabean中这么接受请求?    


问题二:

在javabean中这么写引进smartupload包?




[解决办法]
问题一

什么意思

问题二

直接导入import 就可以 然后吧smart的jar放到web-inf/lib
[解决办法]
没用过html的这个标签用的struts的 <html:file/> ,如果需要的话可以给你参考一下
[解决办法]
一、应该在Action里面接收;
org.apache.struts.upload.FormFile zipFile = (FormFile) form.get( "file ");
二、....
[解决办法]
我也不明白如何在javabean里面接收,

不过你可以在jsp里面用org.apache.struts.upload.FormFile file = (FormFile) request.getAttribute( "file ");
然后把
file.getFileData()传递给javabean进行处理。
[解决办法]
现在大多数上传处理都是写成jsp文件 其中用了smartupload包

<%@ page contentType= "text/html;charset=GBK " %>
<html>
<head>
<title> 文件上传 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>
<body>
<FORM METHOD= "POST " ACTION= "action_upload.jsp " ENCTYPE= "multipart/form-data ">
<p> 请选择要上传的文件
<p> <input type= "FILE " name= "FILE1 " size= "30 ">
<p> <input type= "FILE " name= "FILE2 " size= "30 ">
<p> <input type= "FILE " name= "FILE3 " size= "30 ">
<p> <input type= "FILE " name= "FILE4 " size= "30 ">
<p> <input type= "submit " value= "上传 ">
</FORM>
</body>
</html>

<%@ page contentType= "text/html;charset=GBK " %>
<html>
<head>
<title> 文件上传处理页面 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>

<body>
<jsp:useBean id= "su " scope= "page " class= "com.jspsmart.upload.SmartUpload " />
<%
su.initialize(pageContext);
su.upload();
int count = su.save( "/upload ", su.SAVE_VIRTUAL);
out.println(count+ "个文件上传成功! <br> ");
for (int i=0;i <su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if (file.isMissing()) continue;
out.println( " <br> 文件名: " + file.getFileName()+ " 长度: "+file.getSize());
}
%>
</body>
</html>


我的意思是这样 action_upload.jsp 文件写成javaBean

upload.html文件写成upload.jsp

其中
<jsp:useBean id= "upload " scope= "page " class= "upload " />

上传文件

这问题该有普遍实用意义

请个位老大动动脑 谢谢了


------解决方案--------------------


在javabean里面导入smartupload包就可以用了
[解决办法]
我试写了 op_upload.java

package cgx.run;

import java.sql.*;
import java.util.Vector;
import cgx.util.*;
import cgx.op.*;
import com.jspsmart.upload.*;
import javax.servlet.http.HttpServletRequest;

public class op_upload extends DataBase{
private upload acgs = new upload(); //新的订单类
private javax.servlet.http.HttpServletRequest request; //建立页面请求
private boolean sqlflag = true ; //对接收到的数据是否正确
private Vector uploadlist; //显示订单列表向量数组

public String sqlStr= " ";
public Vector getuploadlist() {
return uploadlist;
}
public boolean getSqlflag() {
return sqlflag;
}
public String to_String( String str) {
try
{
return new String(str.getBytes( "ISO8859-1 "));
}
catch (Exception e)
{
return str;
}
}
/**
* 将页面表单传来的资料分解
*/
public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
boolean flag = false;
try{
request = newrequest;
String ID = request.getParameter( "id ");
long id = 0;
try{
id = Long.parseLong(ID);
}catch (Exception e){
}
acgs.setId(id);

String FILE1 = request.getParameter( "FILE1 ");
if (FILE1==null || FILE1.equals( " ")){FILE1 = "- ";sqlflag = false;}
acgs.setFILE1(to_String(FILE1));

String FILE2 = request.getParameter( "FILE2 ");
if (FILE2==null || FILE2.equals( " ")){FILE2 = "- ";}
acgs.setFILE2(to_String(FILE2));

String FILE3 = request.getParameter( "FILE3 ");;
if (FILE3==null||FILE3.equals( " ")){FILE3 = "- ";sqlflag = false;}
acgs.setFILE3(to_String(FILE3));



String FILE4 = request.getParameter( "FILE4 ");
if (FILE4 == null||FILE4.equals( " ")){FILE4 = "- ";}
acgs.setFILE4(to_String(FILE4));





if (sqlflag)
{
flag = true;
}
return flag;
}catch (Exception e){
return flag;
}
}

/**
* 完成采购单添加
* @return
* @throws java.lang.Exception
*/
public boolean upload() throws Exception {
com.jspsmart.upload.SmartUpload.initialize(pageContext);
com.jspsmart.upload.SmartUpload.op_upload();
int count = com.jspsmart.upload.SmartUpload.save( "/upload ", com.jspsmart.upload.SmartUpload.SAVE_VIRTUAL);
out.println(count+ "个文件上传成功! <br> ");
for (int i=0;i <com.jspsmart.upload.SmartUpload.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = com.jspsmart.upload.SmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
out.println( " <br> 文件名: " + file.getFileName()+ " 长度: "+file.getSize());
}

public op_upload() {
}
}


编译没通过 不知道这么写才对

热点排行