如何获得jsp中file表单的值

yizhizouxiaqu 2009-05-24 10:47:30
我在做一个java mail 的客户端,想从表单上获得文件的路径,但是取得的总是文件的名字,我使用的是jspsmart,请大家帮帮忙!!
代码如下:
写邮件的:

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>
写邮件内容
</title>

</head>

<body>
<form enctype="multipart/form-data" action="sendmailFile.jsp" name="form1" method="post">
<table width="400" border="1" align="center" cellpadding="1">
<tr>
<td width="34%">收信人地址:</td>
<td width="68%">
<input name="to" type="text" id="to">
</td>
</tr>
<tr>
<td>主题:</td>
<td>
<input name="title" type="text" id="title">
</td>
</tr>
<tr>
<td>
<label>
<input type="file" name="uploadfile" id="file"/>
</label>
</td>
</tr>
<tr>
<td height="1" colspan="2">
<textarea name="content" rows="25" cols="70" id="content">
</textarea>
</td>
</tr>
<tr align="center">
<td colspan="2" >
<input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重输">
</td>
</tr>
</table>
</form>
</body>
</html>


处理邮件的:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.*,javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.net.*" %>

<%@ page import="com.jspsmart.upload.*" %>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="java.io.InputStream"%>

<%!
public String codeToString(String str)
{
String s=str;
try
{
byte tempB[] = s.getBytes("utf-8");
s = new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>发送邮件的结果</title>
</head>
<body>
<%

try{
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
String to_mail = codeToString(su.getRequest().getParameter("to"));
String to_title = codeToString(su.getRequest().getParameter("title"));
String to_content = codeToString(su.getRequest().getParameter("content"));


//简历邮件会话
Properties props = new Properties();
props.put("mail.smtp.host","smtp.sina.com");
props.put("mail.smtp.auth","true");
Session s = Session.getInstance(props);
s.setDebug(true);

//由邮件会话新建的一个消息对象
MimeMessage message = new MimeMessage(s);

//设置邮件
InternetAddress from = new InternetAddress("xdh4025@sina.com");
message.setFrom(from);

InternetAddress to = new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(to_title);
message.setSentDate(new Date());

BodyPart mdp = new MimeBodyPart();
mdp.setContent(MimeUtility.encodeText(to_content), "text/html;charset=gb2312");
Multipart mm = new MimeMultipart();
mm.addBodyPart(mdp);
mdp = new MimeBodyPart();
DataHandler dh = new DataHandler("javamail附件测试","text/plain;charset=utf-8");
mdp.setFileName("fj.txt");
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);

mdp = new MimeBodyPart();


String filename = null;

for(int i=0; i<su.getFiles().getCount(); i++)
{

File file = su.getFiles().getFile(i);
filename = file.getFilePathName();
System.out.println("路径名称:"+filename);
}

String filePath = request.getParameter("uploadfile");
String ss = application.getRealPath("uploadfile");


FileDataSource fds = new FileDataSource(filePath);

dh = new DataHandler(fds);
mdp.setFileName("story.txt");
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
message.setContent(mm);
message.saveChanges();

Transport transport = s.getTransport("smtp");
transport.connect("smtp.sina.com", "xdh4025", "123456");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
%>
<div align="center">
<p>发送成功</p>
</div>
<%
}
catch(Exception e)
{
out.print(e);
out.print("发送失败");
}
%>
</body>
</html>

...全文
138 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yizhizouxiaqu 2009-05-25
  • 打赏
  • 举报
回复
谢谢各位,这个应该先上传到服务器指定的文件夹上,然后再取出来!!
jinchun1234 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yizhizouxiaqu 的回复:]
引用 5 楼 qiheia 的回复:
你首先要上传成功才可以获取到路径

请问那我如何才能上传成功呢,
我已经使用了su.upload();
[/Quote]
看看你有没有上传到对应服务器的位置不就可以了!
看下这个例子http://blog.csdn.net/jinchun1234/archive/2009/05/20/4202950.aspx
win720520 2009-05-25
  • 打赏
  • 举报
回复
UP UP 丿
yizhizouxiaqu 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qiheia 的回复:]
你首先要上传成功才可以获取到路径
[/Quote]
请问那我如何才能上传成功呢,
我已经使用了su.upload();
qiheia 2009-05-24
  • 打赏
  • 举报
回复
你首先要上传成功才可以获取到路径
yizhizouxiaqu 2009-05-24
  • 打赏
  • 举报
回复
这个我试过了,得到的值还是null;我的
File file = su.getFiles().getFile(i);
filename = file.getFilePathName();
这两句话就是那个意思,但是打印出来的还是null
qiheia 2009-05-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lllwwt 的回复:]
String myFilepath=myFile.getFilePath();
[/Quote]
LS的就可以了
lllwwt 2009-05-24
  • 打赏
  • 举报
回复
String myFilepath=myFile.getFilePath();
lllwwt 2009-05-24
  • 打赏
  • 举报
回复
su.upload();

com.jspsmart.upload.File myFile = su.getFiles().getFile(0);

81,092

社区成员

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

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