关闭

Android中的图片上传问题

624人阅读 评论(0) 收藏 举报
分类:

Android中的图片上传问题

在Eclipse for JavaEE上搭建一个基于tomcat7.0的Servlet3.0动态网页工程。完成文件提交的服务器部分。Servlet3.0对文件上传提供了很好的支持,仅仅几行代码就能够完成上传。

新建index.jsp,完成服务器上传表单。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

        <form action="Upload" method="post" enctype="multipart/form-data">
            <input type="file" name="file"><br>
            <input type="submit" value="submit"><br>
        </form>

</body>
</html>

新建一个Servle文件名为upload.java,完成服务器端的上传逻辑。

package www.imooc.com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import sun.awt.RepaintArea;

/**
 * Servlet implementation class Upload
 */
 //通过注释来设置文件下载到服务器(也就是我们自己的电脑)的哪个位置
@WebServlet("/Upload")
@MultipartConfig(location="G:\\")
public class Upload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Upload() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //在Servlet3.0下,文件的上传用下面几行代码就能实现
        Part part = request.getPart("file");
        part.write("Sky.jpg");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        out.print("Upload Success!");
        System.out.println("Upload Success!");

    }
}

未完待续

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:128424次
    • 积分:1621
    • 等级:
    • 排名:千里之外
    • 原创:60篇
    • 转载:19篇
    • 译文:0篇
    • 评论:15条
    最新评论