web端、android端的文件上传
来源:互联网 发布:五毛钱特效app软件 编辑:程序博客网 时间:2024/05/22 13:00
1、web端的文件上传。
这里是利用了第三方的jar包。这里所需要的jar包我已经上传到本博客的资源里了,以下是连接
http://download.csdn.net/detail/caihongshijie6/6239041
代码如下:
1)login.jsp
<%@ page contentType="text/html; charset=utf-8"%><html> <body> <form action="http://192.168.1.101:8080/Web3/LoginServlet" method="post" enctype="multipart/form-data"> file:<input name="file" type="file"/><br> <input type="submit"/> </form> </body></html>
2)LoginServlet
package com.njupt.servlet;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class LoginServlet extends HttpServlet {public LoginServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {boolean isMultipart = ServletFileUpload.isMultipartContent(request);try {if (isMultipart) {FileItemFactory factory = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(factory);List<FileItem> items = upload.parseRequest(request);String path = request.getSession().getServletContext().getRealPath("/files1");System.out.println(path);File dir = new File(path);if (!dir.exists()) {dir.mkdirs();}for (FileItem item : items) {if (item.isFormField()) {} else {String filename = item.getName();File file = new File(dir, getFileName(filename));item.write(file);}}} else {doGet(request, response);}} catch (Exception e) {e.printStackTrace();}}public String getFileName(String filename) {if (filename.contains("\\")) {return filename.substring(filename.lastIndexOf("\\") + 1);}return filename;}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}public void init() throws ServletException {// Put your code here}}
2、android文件上传功能的实现
这里也用到了第三方的jar包
下载链接:http://download.csdn.net/detail/caihongshijie6/6239103
public boolean uploadFile(String path , String username , String password , String filename) throws Exception{System.out.println("。。。。。自己写的android短的上传文件.......");org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();PostMethod postMethod = new PostMethod(path); Part[] parts = new Part[]{new StringPart("username",username),new StringPart("password",password),new FilePart("file",new File(filename))};MultipartRequestEntity entity = new MultipartRequestEntity(parts, postMethod.getParams());postMethod.setRequestEntity(entity);int responseCode = httpClient.executeMethod(postMethod);if(responseCode == 200){return true;}return false;}
- web端、android端的文件上传
- Android端上传文件到Web服务器
- web端文件上传功能的思考
- Android端使用HttpUrlConnection模仿Web浏览器实现文件上传
- ios上传的文件在web和Android端旋转90度解决方法
- 实现Web文件的上传
- Web文件上传的草案
- web文件上传的原理
- java web的文件上传
- Web里面的文件上传
- java web文件的上传
- Android 文件的上传
- Android文件的上传
- Android 文件的上传
- android手机端上传指定路径的文件
- 文件上传原理:Web上传文件的原理及实现
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- SVN的使用记录
- Kendo UI开发教程(27): 移动应用开发简介
- Android,谁动了我的内存(1)
- 面试之前必看之联合、strcpy string
- 国外著名java技术资料网站
- web端、android端的文件上传
- 解决折线图动态渐变-感悟
- linux下如何获取cpu的利用率
- Kendo UI开发教程: Kendo UI 示例及总结
- 关于四字节对齐的问题
- 关于本博客 http://www.imobilebbs.com
- 关于android微信分享的开发
- 套接字API(十四)write和read
- C++ 类的继承