java 檔案上傳

來源:互聯網
上載者:User

標籤:檔案的   .com   ring   上傳   擷取   auth   uid   建立   pac   

package servlet;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;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.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import entit.News;import service.NewService;import service.impl.NewServiceImpl;/** * Servlet implementation class newsServlet */@WebServlet("/uploadFileServlet")public class newsServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public newsServlet() {        super();    }    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        String uplaodPath = null;        response.setCharacterEncoding("utf-8");        News news = new News();         DiskFileItemFactory factory = new DiskFileItemFactory();        ServletFileUpload upload = new ServletFileUpload(factory);        boolean flag = upload.isMultipartContent(request);        if (flag) { // 證明檔案上傳請求            try {                // 擷取的是所有的表單元素 包括普通元素和檔案元素                List<FileItem> items = upload.parseRequest(request);                // List<FileItem> 是一個元素嗎? 是4個??有可能N個??? 遍曆!!!!                Iterator<FileItem> its = items.iterator();                while (its.hasNext()) { // 遍曆集合                    // 擷取一個一個的元素                    FileItem item = its.next();                    if (item.isFormField()) { // 普通欄位                        String fileName = item.getFieldName(); // 擷取name屬性值                        if (fileName.equals("id")) {                            news.setNewsid(Integer.parseInt(item.getString("utf-8")));                        } else if (fileName.equals("title")) {                            news.setNewstitle(item.getString("utf-8"));                        } else if (fileName.equals("author")) {                            news.setNewssummary(item.getString("utf-8"));                        }                    } else { // 檔案元素                        // 擷取伺服器所在的位置                        uplaodPath = request.getSession().getServletContext().getRealPath("uploadFiles/");                                                File file = new File(uplaodPath);                        if (!file.exists()) { // 如果建立檔案夾不存雜 則建立                            file.mkdirs();                        }                        // 擷取上傳檔案的名稱                        String fileName = item.getName();                        // 使用者有可能不上傳檔案                        if (!fileName.equals("") && fileName != null) {                            File uploadFile = new File(fileName); // 擷取上傳檔案                            File saveFile = new File(uplaodPath,                                    uploadFile.getName());// 拼接檔案位置                            // 真正的上傳                            item.write(saveFile);                            news.setNewspic(uploadFile.getName()); // 給對象賦值                        }                    }                }                NewService service = new NewServiceImpl();                int num = service.addNew(news);                if (num > 0) {                    System.out.println(uplaodPath);                } else {                    System.out.println("新增失敗");                }            } catch (Exception e) {                e.printStackTrace();            }        } else {            System.out.println("不是檔案上傳請求!");        }    }}

使用的時候需要匯入倆個jar

java 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.