资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么发送JAVA源代码 如何发布java程序

Java mail 发送邮件例子 源代码

晕,貌似这个问题以前有人提过,我也回答过了,不满意吗?需要用到javamail的jar包,网上有。找不到把邮箱贴出来,我发给你。package test.servlet;import java.io.IOException;

创新互联主打移动网站、成都做网站、成都网站设计、成都外贸网站建设、网站改版、网络推广、网站维护、国际域名空间、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。

import java.io.PrintWriter;

import java.util.Date;

import java.util.Properties;import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;public class SendMailServlet extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 2960000940515881314L;

private ServletConfig config = null;

private static final String CONTENT_TYPE = "text/html";public void init(ServletConfig config1) throws ServletException {

this.config = config1;

}final public ServletConfig getServletConfig() {

return config;

}/**

* Constructor of the object.

*/

public SendMailServlet() {

super();

}/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy();

}/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String to_mail=codeToString(request.getParameter("to"));

String to_title=codeToString(request.getParameter("title"));

String to_content=codeToString(request.getParameter("content"));

Properties props=new Properties();//也可用Properties props = System.getProperties(); //

props.put("mail.smtp.host","localhost");//存储发送邮件服务器的信息

props.put("mail.smtp.auth","true");//同时通过验证

Session s=Session.getInstance(props);//根据属性新建一个邮件会话

s.setDebug(true);//由邮件会话新建一个消息对象

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

InternetAddress from;

try {

from = new InternetAddress("06254054@bjtu.mail.com");

message.setFrom(from);//设置发件人

InternetAddress to=new InternetAddress(to_mail);

message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO

message.setSubject(to_title);//设置主题

message.setText(to_content);//设置信件内容

message.setSentDate(new Date());//设置发信时间

//发送邮件

message.saveChanges();//存储邮件信息

Transport transport=s.getTransport("smtp");

//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码

transport.connect("localhost",8479,"","");

transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址

transport.close();

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

out.print("success");

} catch (AddressException e) {

e.printStackTrace();

} catch (MessagingException e) {

e.printStackTrace();

}}

public String codeToString(String str) {//处理中文字符串的函数

String s=str;

try {

byte tempB[]=s.getBytes("ISO-8859-1");

s=new String(tempB);

return s;

} catch(Exception e) {

return s;

}

}

}

怎样导入Java源程序?

1,直接拷贝覆盖,不管是完整的带项目配置文件的java源程序,还是不完整的或缺少配置文件的都可以。\x0d\x0a具体步骤:\x0d\x0a先在Eclipse中新建空的工程并且命好名,然后把旧工程的目录里所有代码文件目录,直接拷贝覆到新建空的工程工程目录下,然后在eclipse中右击刷新就可以了。\x0d\x0a比如:新建工程名叫xxx,需要复制的工程文件名叫sss\x0d\x0a在本地磁盘打开xxx所在的文件,打开xxx目录,然后把sss目录下的所有文件复制到xxx目录下就可以了。在eclipse中刷新,再导入需要的jar包就行了。\x0d\x0a\x0d\x0a2,对于完整的带项目配置文件的java源码,按步骤操作即可:\x0d\x0aFile-Import-General\x0d\x0a选择ExistingProjectsintoWorkspace,选择要导入的文件,点击“finish",OK。\x0d\x0a\x0d\x0a3,对于不完整的工程(java程序源文件)或缺少工程配置文件的,\x0d\x0a先新建空的工程并且命好名\x0d\x0a然后File-Import-General\x0d\x0a选择FileSystem,点击“next”,Fromdirectory选择要导入的路径,\x0d\x0a注意intoFolder要选择刚刚建好的工程路径,点击“finish",OK。

如何把java的web工程发布到另外一台服务器上

JDK 1.4.2 + Myeclipse 6.0(实在经不起诱惑,尝尝鲜) + Tomcat 5.0.28 + AXIS 1.4。 AXIS 1.4包可以在 找到。 假设所有的环境你已经搭好,并且AXIS包也已经下好了。OK, Here we go~

解压axis-bin-1_4.zip这个包可以看到webapps目录,双击进入把里面的AXIS文件夹拷到 %TOMCAT_HOME%\webapps目录下,之后拷贝activation.jar、mail.jar、tools.jar到% TOMCAT_HOME%\webapps\axis\WEB-INF\lib目录下。启动tomcat,访问 如果访问成功,恭喜你!基本的配置你已经做完了。

PS:此处的%TOMCAT_HOME%指的是Tomcat的安装目录,至于那另外的三个jar包,J2EE 1.4库里就能找的到。

现在来说一下最关键的Webservice的发布。AXIS提供了两种发布方式,一种是即时发布(Instant Deployment),另外一种是定制发布(Custom Deployment)。即时发布提供了一种非常简单的webservice的发布方式,但是其中限制太多,因此在实际的开发中定制发布才是首选。这里也将会以定制发布为重点来介绍。

1.即时发布 JWS (Java Web Service) Files - Instant Deployment

即时发布提供了一种非常简单发布方式,发布者只要有Java源代码(也就是.java文件),然后把其后缀名改成jws(也就是 java web service的缩写)拷贝到%TOMCAT_HOME%\webapps\axis目录下即完成了所有的发布工作。AXIS的编译引擎会处理接下来的所有事情。下面是一段示例代码:

java 代码

1. public class HelloAXIS {

2.

3. public String Hello(String name){

4. return "AXIS say hello to " + name;

5. }

6. }

把 HelloAXIS.java 文件改成 HelloAXIS.jws 然后拷贝到%TOMCAT_HOME%\webapps\axis目录下,启动Tomcat之后访问 如果能看到 Click to see the WSDL这个超链接就说明已经发布成功了,点击进去就可以看到这个Webservice的WSDL描述文件。server端的发布已经完成了,接下来就是编写Client端测试代码了。

java 代码

1. package com.chnic.test;

2.

3. import java.net.URL;

4.

5. import javax.xml.namespace.QName;

6.

7. import org.apache.axis.client.Call;

8. import org.apache.axis.client.Service;

9.

10. public class Test {

11.

12. public static void main(String[] args) throws Exception{

13. String targetEendPoint = "" ;

14. Service service = new Service();

15. Call call = (Call) service.createCall();

16. call.setOperationName( new QName(targetEendPoint, "Hello" ));

17. call.setTargetEndpointAddress( new URL(targetEendPoint));

18. String result = (String) call.invoke( new Object[]);

19. System.out.println(result);

20. }

21. }

测试代码很简单,如果熟悉java反射机制的朋友不用两分钟就能看明白。运行后客户端控制台出现 AXIS say hello Robert。测试成功。果然很简单吧。不过在这简单背后却是以牺牲灵活性为代价的。假如你现在手里只有.class 或者一个jar包,jws就不再能满足你的需求了,最要命的就是即时发布不支持带包的类,这点AXIS的用户手册上写的也很明白。

Important: JWS web services are intended for simple web services. You cannot use packages in the pages, and as the code is compiled at run time you can not find out about errors until after deployment. Production quality web services should use Java classes with custom deployment.

2.定制发布 Custom Deployment - Introducing WSDD

比起即时发布定制发布更加烦琐也更复杂,但是换来的却是更大的灵活性,因此在实际项目中定制发布还是不二的选择。定制发布需要你自己编写一个WSDD (Web Service Deployment Descriptor)文件,这个东东类似与XML稍后会做出介绍。废话不多说,我们来看代码:

java 代码

1. package com.chnic.webservice;

2.

3. public class HelloWorld {

4.

5. public HelloWorld(){

6.

7. }

8.

9. public String hello(String str){

10. return "Hello " + str;

11. }

12.

13. public int add( int a, int b){

14. return a + b;

15. }

16.

17. }

一个带包的很简单的类,在eclipse下编译后按照包名拷到 %TOMCAT_HOME%\webapps\axis\WEB-INF\classes 目录下。以这个类为例,拷贝完之后这个HelloWorld.class的路径就是 %TOMCAT_HOME%\webapps\axis\WEB-INF\classes\com\chnic\webservice。PS: 如果嫌这样太麻烦,可以另外建一个Java Web工程用myeclipse的发布工具发布到Tomcat之后,整体一次性拷贝到websericve的工程中。

接下来就需要编写发布文件deploy.wsdd。到%TOMCAT_HOME%\webapps\axis\WEB-INF 目录下建立这个文件并在其中添加如下内容:

xml 代码

1. deployment xmlns =

2. xmlns:java = ""

3.

4. service name = "HelloWorld" provider = "java:RPC"

5. parameter name = "className" value = "com.chnic.webservice.HelloWorld" /

6. parameter name = "allowedMethods" value = "*" /

7. / service

8. / deployment

简单的介绍下各个节点的含义,"HelloWorld"当然是这个webservice的名字,后面紧跟的java:RPC指的是服务类型。这里一共有有4种类型,分别是:RPC, Document, Wrapped 和 Message。有兴趣可以看下 org.apache.axis.providers这个包和子包下面的类的API文档。之后的parameter节点第一个当然是指出具体的类,第二个从字面上也很好理解:允许调用的方法。这里的配置告诉引擎可以调用所有的public方法,当然你也可以自己指定。

编写完配置发布文件之后,cmd打开windows的控制台,进入%TOMCAT_HOME%\webapps\axis\WEB-INF目录下键入如下命令

1. java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient deploy.wsdd

之后控制台返回Processing file deploy.wsdd 和 Done processing 这两段话即说明发布成功。(此时会在同级目录生成一个server-config.wsdd文件 )在这里的AdminClient是AXIS提供的一个客户端管理工具。至于java.ext.dirs的含义可以去了解一下classloader和JVM类装载机制方面的知识,在这里就不多解释。 还有一点要注意的是在发布的时候Tomcat服务必须处于启动状态,否则就会抛出一堆无法连接的异常信息。发布成功之后你可以通过访问 来查看你所有的定制发布的服务。

客户端测试代码

1. String targetEendPoint = "" ;

2. Service service = new Service();

3. Call call = (Call) service.createCall();

4. call.setTargetEndpointAddress( new URL(targetEendPoint));

5.

6. call.setOperationName( new QName(targetEendPoint, "hello" ));

7. String result = (String) call.invoke( new Object[]);

8. System.out.println(result);

9.

10. call.setOperationName( new QName(targetEendPoint, "add" ));

11. Integer res = (Integer) call.invoke( new Object[]);

12. System.out.println( "The result is: " + res);

运行测试代码,控制台显示 Hello Robert 和 The result is: 3 这两句话,说明发布成功。仔细观察下发现其实除了那个targetEndpoint 之外,即时发布和定制发布的客户端调用代码基本上都是一样的。定制发布的URL可以在WSDL文件里找到。

java源代码如何用

java源代码是用来关联jar中的编译代码的。

应用场景:通常在打开eclipse中的jar的时候,发现class文件不能被打开,此时出现下面的界面,此时就需要通过“Attach Source”,之后找到对应的java源代码来实现代码关联,即可正常的显示class类中的内容。

备注:如果此处ava源代码指的是源代码文件(“.java”),是用来进行代码维护和二次开发的必备东西。

求用java编写的文件上传源代码

先放好jspsmartupload包

%@ page contentType="text/html;charset=GB2312" %

%@ page language="java" import="com.jspsmart.upload.*"%

HTML

head

title文件上传处理页面/title

/head

BODY

p align="center"jspSmartUpload组件应用之员工档案/p

HR

%

// 新建一个SmartUpload对象

SmartUpload su = new SmartUpload();

// 上传初始化

su.initialize(pageContext);

// 设定允许上传的文件(通过扩展名限制),仅允许jpg,bmp,gif文件。

su.setAllowedFilesList("jpg,bmp,gif");

// 上传文件

su.upload();

// 将上传文件全部保存到指定目录

su.save("/upload");

%

p align="center"

table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474"

tr

td width="150"div align="left"psmallfont face="Verdana"姓名:  /font/small/td

td width="324"smallfont face="Verdana"

%=su.getRequest().getParameter("username")%

br/font/small/td

/tr

tr

tdsmallfont face="Verdana"照片:  /font/small/td

tdsmallimage src="%="upload/"+su.getFiLes().getFile(0).getFileName()%"/td

/tr

tr

tdsmallfont face="Verdana"年龄: /font/small/td

tdsmallfont face="Verdana"%=su.getRequest().getParameter("age")%/font/small/td

/tr

tr

tdsmallfont face="Verdana"工作单位: /font/small/td

tdsmallfont face="Verdana"%=su.getRequest().getParameter("department")%/font/small/td

/tr

tr

td colspan="2" width="474"div align="center"centerpsmallfont face="Verdana"

input TYPE="Submit" value="提交" /font/small/td

/tr

/table

/p

/BODY

/HTML

java如何发送短信息,要源码

已加你好友,代码发送大8681146@qq.com,我的是1160061396@qq.com,请注意查收……


网站标题:怎么发送JAVA源代码 如何发布java程序
本文路径:http://cdkjz.cn/article/hgjhei.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220