<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • 上傳下載文件java代碼 上傳下載文件java代碼錯誤

            用java實現文件的上傳與下載

            1.下載簡單,無非是把服務器上的文件或者數據庫中的BLob(或其他二進制型),用流讀出來,然后寫到客戶端即可,要注意 ContentType。

            成都創(chuàng)新互聯公司服務項目包括嘉祥網站建設、嘉祥網站制作、嘉祥網頁制作以及嘉祥網絡營銷策劃等。多年來,我們專注于互聯網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯網行業(yè)的解決方案,嘉祥網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到嘉祥省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

            2.上傳,可以用Apache Commons Upload等開源工具,或者自己寫:

            form要用enctype="multipart/form-data"

            然后服務器端也是用IO把客戶端提交的文件流讀入,然后寫到服務器的文件系統(tǒng)或者數據庫里。不同的數據庫對Lob字段操作可能有所不同,建議用Hibernate,JPA等成熟的ORM框架,可以不考慮數據庫細節(jié)。

            用Java的三大框架實現文件的上傳下載,求代碼啊,最好是分為action,service,serv

            package cn.itcast.struts2.demo1;

            import java.io.File;

            import org.apache.commons.io.FileUtils;

            import org.apache.struts2.ServletActionContext;

            import com.opensymphony.xwork2.ActionSupport;

            /**

            * 完成文件上傳 (不是解析上傳內容,因為上傳內容 由fileUpload攔截器負責解析)

            *

            * @author seawind

            *

            */

            public class UploadAction extends ActionSupport {

            // 接收上傳內容

            // input type="file" name="upload" /

            private File upload; // 這里變量名 和 頁面表單元素 name 屬性一致

            private String uploadContentType;

            private String uploadFileName;

            public void setUpload(File upload) {

            this.upload = upload;

            }

            public void setUploadContentType(String uploadContentType) {

            this.uploadContentType = uploadContentType;

            }

            public void setUploadFileName(String uploadFileName) {

            this.uploadFileName = uploadFileName;

            }

            @Override

            public String execute() throws Exception {

            if (upload == null) { // 通過xml配置 required校驗器 完成校驗

            // 沒有上傳文件

            return NONE;

            }

            // 將上傳文件 保存到服務器端

            // 源文件 upload

            // 目標文件

            File destFile = new File(ServletActionContext.getServletContext()

            .getRealPath("/upload") + "/" + uploadFileName);

            // 文件復制 使用commons-io包 提供 工具類

            FileUtils.copyFile(upload, destFile);

            return NONE;

            }

            }

            多文件上傳

            package cn.itcast.struts2.demo1;

            import java.io.File;

            import org.apache.commons.io.FileUtils;

            import org.apache.struts2.ServletActionContext;

            import com.opensymphony.xwork2.ActionSupport;

            /**

            * 支持多文件上傳

            *

            * @author seawind

            *

            */

            public class MultiUploadAction extends ActionSupport {

            // 接收多文件上傳參數,提供數組接收就可以了

            private File[] upload;

            private String[] uploadContentType;

            private String[] uploadFileName;

            public void setUpload(File[] upload) {

            this.upload = upload;

            }

            public void setUploadContentType(String[] uploadContentType) {

            this.uploadContentType = uploadContentType;

            }

            public void setUploadFileName(String[] uploadFileName) {

            this.uploadFileName = uploadFileName;

            }

            @Override

            public String execute() throws Exception {

            for (int i = 0; i upload.length; i++) {

            // 循環(huán)完成上傳

            File srcFile = upload[i];

            String filename = uploadFileName[i];

            // 定義目標文件

            File destFile = new File(ServletActionContext.getServletContext()

            .getRealPath("/upload" + "/" + filename));

            FileUtils.copyFile(srcFile, destFile);

            }

            return NONE;

            }

            }

            java實現文件的上傳和下載

            用輸出流 接受 一個下載地址的網絡流

            然后將這個輸出流 保存到本地一個文件 后綴與下載地址的后綴相同··

            上傳的話 將某個文件流 轉成字節(jié)流 上傳到某個webservice方法里

            -------要代碼來代碼

            URL url=new URL("");

            URLConnection uc=url.openConnection();

            InputStream in=uc.getInputStream();

            BufferedInputStream bis=new BufferedInputStream(in);

            FileOutputStream ft=new FileOutputStream("E://1.rar");

            這是下載 上傳太麻煩就不給寫了

            java 文件上傳下載的代碼

            FileInputStream fin = new FileInputStream(new File("你的文件地址"));

            OutputStream out = 你的目標流地址,可以是Socket的Output流,也可以是http的Output流,等等

            byte[] b = new byte[65535]; // 一次讀取多少字節(jié)

            int read = -1;

            while(-1 != (read = fin.read(b))){

            out.write(b,0,read);

            }

            求一java文件上傳下載的主要代碼,非網頁的,最好關鍵地方能有說明

            利用struts2的上傳下載

            package?com.java.action;

            import?java.io.File;

            import?java.io.FileInputStream;

            import?java.io.FileNotFoundException;

            import?java.io.IOException;

            import?java.io.InputStream;

            import?java.io.UnsupportedEncodingException;

            import?java.net.URLEncoder;

            import?org.apache.commons.io.FileUtils;

            import?org.apache.struts2.ServletActionContext;

            import?com.opensymphony.xwork2.ActionSupport;

            public?class?FileAction?extends?ActionSupport?{

            /**

            *?用于上傳的變量

            */

            //封裝該文件域對應的文件內容

            private?File[]?upload;

            //封裝該文件域對應的文件的文件名

            private?String[]?uploadFileName;

            //封裝該文件域對應的文件的文件類型

            private?String[]?uploadContentType;

            /**

            *?用于下載的變量

            */

            private?String[]?fileNames;

            private?String?fileName;

            /**

            *?設置getter和setter

            *?@return

            */

            public?String[]?getFileNames()?{

            return?fileNames;

            }

            public?File[]?getUpload()?{

            return?upload;

            }

            public?void?setUpload(File[]?upload)?{

            this.upload?=?upload;

            }

            public?String[]?getUploadFileName()?{

            return?uploadFileName;

            }

            public?void?setUploadFileName(String[]?uploadFileName)?{

            this.uploadFileName?=?uploadFileName;

            }

            public?String[]?getUploadContentType()?{

            return?uploadContentType;

            }

            public?void?setUploadContentType(String[]?uploadContentType)?{

            this.uploadContentType?=?uploadContentType;

            }

            public?void?setFileNames(String[]?fileNames)?{

            this.fileNames?=?fileNames;

            }

            public?String?getFileName()?{

            return?fileName;

            }

            public?void?setFileName(String?fileName)?{

            this.fileName?=?fileName;

            }

            /**

            *?用于上傳文件的方法

            *?@return

            */

            public?String?upload(){

            //設置文件上傳到的位置

            String?path?=?ServletActionContext.getServletContext().getRealPath("/file");

            //設置文件目標

            try?{

            for?(int?i?=?0;?i??upload.length;?i++)?{

            File?target?=?new?File(path,?uploadFileName[i]);

            FileUtils.copyFile(upload[i],?target);

            }

            return?SUCCESS;

            }?catch?(IOException?e)?{

            e.printStackTrace();

            }

            return?INPUT;

            }

            /**

            *?得到所有上傳的文件的名稱

            *?@return

            */

            public?String?fileList(){

            String?path?=?ServletActionContext.getServletContext().getRealPath("/file");

            fileNames?=?new?File(path).list();

            return?SUCCESS;

            }

            /**

            *?用于下載文件的方法

            *?@return

            */

            public?InputStream?getInputStream(){

            if(fileName==null?||?fileName.isEmpty())?return?null;

            String?path?=?ServletActionContext.getServletContext().getRealPath("/file");

            try?{

            return?new?FileInputStream(new?File(path,fileName));

            }?catch?(FileNotFoundException?e)?{

            e.printStackTrace();

            }

            return?null;

            }

            public?String?getContentDisposition(){

            try?{

            if(fileName==null||fileName.isEmpty()){

            return?"inline";

            }

            return?"attachment;filename="+URLEncoder.encode(fileName,?"UTF-8");

            }?catch?(UnsupportedEncodingException?e)?{

            e.printStackTrace();

            }

            return?"inline";

            }

            }

            比java的io方便多了

            JAVA action中如何 上傳 下載文件??

            /**

            上傳文件

            */

            public class FileAction extends Action {

            public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response)

            throws Exception {

            try {

            FileForm fileform = (FileForm) form;

            //取得請求的文件集合

            Hashtable hash = fileform.getMultipartRequestHandler().getFileElements();

            //得到hashtable的枚舉值

            Enumeration enu = hash.elements();

            //如果該枚舉值包含有其它的文件

            while(enu.hasMoreElements()) {

            //得到文件

            FormFile file = (FormFile) enu.nextElement();

            System.out.println(file);

            add(file);

            }

            return mapping.findForward("yes");

            } catch (Exception e) {

            e.printStackTrace();

            }

            return super.execute(mapping, form, request, response);

            }

            public void add(FormFile file){

            try {

            //取得寫文件的目錄

            String url=servlet.getServletContext().getRealPath("upload");

            File f1=new File(url);

            if(!f1.exists()){//如果文件目錄不存在

            f1.mkdirs();//創(chuàng)建目錄

            }

            String fileName=file.getFileName();

            //創(chuàng)建一個文件輸入流

            InputStream is=file.getInputStream();

            OutputStream out=new FileOutputStream(url+"/"+fileName);

            int byteRead=0;

            byte[] by=new byte[8192];

            while((byteRead=is.read(by, 0, 8192))!=-1){

            out.write(by, 0, byteRead);

            }

            out.close();

            is.close();

            file.destroy();

            } catch (Exception e) {

            e.printStackTrace();

            }

            }

            }

            /**

            下載文件

            */

            頁面一開始進去action,action負責把file文件夾下的所有文件讀入一個ArrayList中

            Action代碼如下:

            ArrayList list = new ArrayList();

            String path=request.getRealPath("/")+"file";

            String FullPath;

            //System.out.println(path);

            myDir=new File(path);

            list.clear();

            contents=myDir.listFiles();

            for(int i=0;icontents.length;i++){

            FullPath=contents.getName();

            list.add(FullPath);

            //System.out.println(FullPath);

            }

            request.setAttribute("list",list);

            ActionForward forward=new ActionForward("/download.jsp");

            return forward;

            然后進入download.jsp中,這個頁面主要負責把所有文件顯示,并提供下載連接,代碼如下:

            %@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%

            head

            /style

            /head

            body

            %ArrayList list=(ArrayList)request.getAttribute("list");

            for(int i=0;ilist.size();i++)

            {

            String a=java.net.URLEncoder.encode((String)list.get(i));

            out.print("a href=./loaded.do?name="+a+""+list.get(i)+"/abr");

            }

            %

            /body

            /html

            注意,下劃線畫中的代碼的作用,就是解決問題的所在。

            接下來可以直接傳入到loadedaction中,也可以通過一個form,我演示的是通過一個form

            Form代碼如下

            package org.aeolus.struts.form;

            import javax.servlet.http.HttpServletRequest;

            import org.apache.struts.action.ActionErrors;

            import org.apache.struts.action.ActionForm;

            import org.apache.struts.action.ActionMapping;

            public class LoadForm extends ActionForm {

            /*

            *Generated Methods

            */

            private String name;

            public String getName() {

            return name;

            }

            public void setName(String name) {

            this.name = name;

            }

            }

            接下來就是action的代碼

            LoadForm doc=(LoadForm)form;

            String docName = new String(doc.getName().getBytes("8859_1"));

            File f;

            if(docName!=""){

            String docFullPath=request.getRealPath("/");

            f = new File(docFullPath+"file\\"+docName);

            response.reset();

            response.setContentType("application/x-msdownload;charset=GBK");

            System.out.print(response.getContentType());

            response.setCharacterEncoding("UTF-8");

            docName=java.net.URLEncoder.encode(docName,"UTF-8");

            response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));

            BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));

            byte[] buf = new byte[1024];

            int len = 0;

            OutputStream out = response.getOutputStream();

            while((len = br.read(buf)) 0)

            out.write(buf,0,len);

            out.close();

            response.wait();

            ActionForward forward=new ActionForward("/download.jsp");

            return forward; }

            return null;

            注意,下劃線畫中的代碼的作用,就是解決問題的所在。說明一下:

            response.setCharacterEncoding("UTF-8");

            docName=java.net.URLEncoder.encode(docName,"UTF-8");

            response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));

            如果不這樣做你將要下載的文件名是亂碼。

            當前文章:上傳下載文件java代碼 上傳下載文件java代碼錯誤
            網站地址:http://www.jbt999.com/article0/hgcsoo.html

            成都網站建設公司_創(chuàng)新互聯,為您提供網站收錄、標簽優(yōu)化、面包屑導航、品牌網站設計、外貿建站、微信小程序

            廣告

            聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

            成都網頁設計公司

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 大,香蕉在线伊人在线 | 求一个做爱视频网站免费在线观看 | 日本中文字幕不卡 | 色欲色欲一区二区三区 | 亚洲欧美一区二区三区在线 |