「java文件收发系统」java 发送文件
今天给各位分享java文件收发系统的知识,其中也会对java 发送文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、Java服务器怎么接收手机上传过来的文件。求具体代码,谢谢,如果觉得悬赏太少的话可以说,我再加。急。。
- 2、求一个javaweb邮件收发系统eclipse源代码,tomcat可以运行的。
- 3、JAVA中如何实现文件分发功能?
- 4、Java如何实现文件系统
Java服务器怎么接收手机上传过来的文件。求具体代码,谢谢,如果觉得悬赏太少的话可以说,我再加。急。。
android客户端代码:
public class MainActivity extends Activity
{
private TextView uploadInfo;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadInfo = (TextView) findViewById(R.id.upload_info);
uploadFile();
}
public void uploadFile()
{
//服务器端地址
String url = "";
//手机端要上传的文件,首先要保存你手机上存在该文件
String filePath = Environment.getExternalStorageDirectory()
+ "/1/power.apk";
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams param = new RequestParams();
try
{
param.put("file", new File(filePath));
param.put("content", "liucanwen");
httpClient.post(url, param, new AsyncHttpResponseHandler()
{
@Override
public void onStart()
{
super.onStart();
uploadInfo.setText("正在上传...");
}
@Override
public void onSuccess(String arg0)
{
super.onSuccess(arg0);
Log.i("ck", "success" + arg0);
if(arg0.equals("success"))
{
Toast.makeText(MainActivity.this, "上传成功!", 1000).show();
}
uploadInfo.setText(arg0);
}
@Override
public void onFailure(Throwable arg0, String arg1)
{
super.onFailure(arg0, arg1);
uploadInfo.setText("上传失败!");
}
});
} catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "上传文件不存在!", 1000).show();
}
}
}
服务器端代码:
public class UploadFileServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 创建文件项目工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置文件上传路径
String upload = this.getServletContext().getRealPath("/upload/");
// 获取系统默认的临时文件保存路径,该路径为Tomcat根目录下的temp文件夹
String temp = System.getProperty("java.io.tmpdir");
// 设置缓冲区大小为 5M
factory.setSizeThreshold(1024 * 1024 * 5);
// 设置临时文件夹为temp
factory.setRepository(new File(temp));
// 用工厂实例化上传组件,ServletFileUpload 用来解析文件上传请求
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
// 解析结果放在List中
try
{
ListFileItem list = servletFileUpload.parseRequest(request);
for (FileItem item : list)
{
String name = item.getFieldName();
InputStream is = item.getInputStream();
if (name.contains("content"))
{
System.out.println(inputStream2String(is));
} else if(name.contains("file"))
{
try
{
inputStream2File(is, upload + "\\" + item.getName());
} catch (Exception e)
{
e.printStackTrace();
}
}
}
out.write("success");
} catch (FileUploadException e)
{
e.printStackTrace();
out.write("failure");
}
out.flush();
out.close();
}
// 流转化成字符串
public static String inputStream2String(InputStream is) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1)
{
baos.write(i);
}
return baos.toString();
}
// 流转化成文件
public static void inputStream2File(InputStream is, String savePath)
throws Exception
{
System.out.println("文件保存路径为:" + savePath);
File file = new File(savePath);
InputStream inputSteam = is;
BufferedInputStream fis = new BufferedInputStream(inputSteam);
FileOutputStream fos = new FileOutputStream(file);
int f;
while ((f = fis.read()) != -1)
{
fos.write(f);
}
fos.flush();
fos.close();
fis.close();
inputSteam.close();
}
}
求一个javaweb邮件收发系统eclipse源代码,tomcat可以运行的。
package me.gacl.main;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 创建一封只包含文本的邮件
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//邮件的标题
message.setSubject("只包含文本的简单邮件");
//邮件的文本内容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回创建好的邮件对象
return message;
}
}
JAVA中如何实现文件分发功能?
这个不用什么技术来做。我说说我的想法。
每个人有个待收件表,当我分发一个文件给某些人的时候,这些人的待收件表就会有数据,数据关联的对象是 要分发的文件(如ID),以及状态(表示接收人查看或接收状态)。
那么,每个接收人一进入这个系统,或者一进入这个待收件模块,系统自动搜索是否有未接收的文件(根据上面说的状态判断),如果有,则弹出提示框。接收人去接收,则根据这个文件(ID)去服务器获取这个文件的下载路径。
希望能帮到你。
Java如何实现文件系统
package com.kiritor.util;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* 文件的相关操作类
*
* @author Kiritor
*/
public class FileOperation {
private static String contentPath;
private static String filePath;
private static File[] fileList = null;// 保存文件列表,过滤掉目录
public FileOperation() {
}
/** 构造函数的参数是一个目录 */
public FileOperation(String path) {
File file = new File(path);
if (file.isDirectory())
this.contentPath = path;
else
this.filePath = path;
}
/**获取文件列表*/
public static File[] getFiles() {
if (contentPath == null) {
File file = new File(filePath);
fileList = new File[1];
fileList[0] = file;
return fileList;
}
fileList = new File(contentPath).listFiles(new FileFilter() {
/**使用过滤器过滤掉目录*/
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory())
{
return false;
}else
return true;
}
});
return fileList;
}
/** 对当前目录下的所有文件进行排序 */
public static File[] sort() {
getFiles();
Arrays.sort(fileList, new FileComparator());
return fileList;
}
public static void tree(File f, int level) {
String preStr = "";
for(int i=0; ilevel; i++) {
preStr += " ";
}
File[] childs = f.listFiles();
//返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
for(int i=0; ichilds.length; i++) {
System.out.println(preStr + childs[i].getName());
if(childs[i].isDirectory()) {
tree(childs[i], level + 1);
}
}
}
// 提供一个"比较器"
static class FileComparator implements java.util.ComparatorFile {
@Override
public int compare(File o1, File o2) {
// 按照文件名的字典顺序进行比较
return o1.getName().compareTo(o2.getName());
}
}
}
java文件收发系统的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 发送文件、java文件收发系统的信息别忘了在本站进行查找喔。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。