中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

使用FileUpload上傳文件的主要Java代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
 
import java.io.File;
import java.io.IOException;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
public class UploadServlet extends HttpServlet {
 
    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("--------------upload-----------");
 
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(1024 * 1024); // 設(shè)置緩存大小,單位KB
        String path = request.getRealPath("/upload"); // 得到當(dāng)前項(xiàng)目upload目錄的絕對(duì)路徑
        factory.setRepository(new File(path)); // 設(shè)置文件臨時(shí)存儲(chǔ)路徑為path
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> list = upload.parseRequest(request);
            for (FileItem item : list) {
                // 如果item是普通表單字段
                if (item.isFormField()) {
                    String name = item.getFieldName(); // 得到表單字段的name屬性值
                    String value = item.getString("gbk"); // 得到表單字段值,編碼采用gbk
                    request.setAttribute(name, value);
                } else {
                    String name = item.getFieldName();
                    String value = item.getName(); // 得到上傳文件的路徑(根據(jù)瀏覽器不同可能會(huì)有所差異)
                    int start = value.lastIndexOf("\\");
                    String fileName = value.substring(start + 1); // 截取最后一個(gè)\之后的字符,即行到上傳文件名
                    request.setAttribute(name, fileName);
 
                    item.write(new File(path, fileName)); // 在硬盤(pán)上寫(xiě)入文件
 
                    // 自己用流寫(xiě)的方法
                    // OutputStream os = new FileOutputStream(new
                    // File(path,fileName));
                    // InputStream is = item.getInputStream();
                    // byte[] buffer = new byte[500];
                    // int length = 0;
                    // while((length = is.read(buffer))>0){
                    // os.write(buffer, 0, length);
                    // }
                    // os.close();
                    // is.close();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        request.getRequestDispatcher("upload/result.jsp").forward(request,
                response);
    }
}

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php操作SVN類(lèi)

下一篇:Python文件的一些操作代碼