永发信息网

java servlet中下载xml类型文件

答案:2  悬赏:0  手机版
解决时间 2021-04-07 17:26
  • 提问者网友:嘚啵嘚啵
  • 2021-04-06 20:30
在servlet中生成了一个xml文件,重定向到文件,想让浏览器提示另存为下载该xml文件,但是现在浏览器直接把这个xml打开了,请问该怎么设置一下,不让浏览器打开而是提示保存,另存为那种形式
最佳答案
  • 五星知识达人网友:西岸风
  • 2021-04-06 21:34
重定向的,不好处理。。。。。。。自己使用流处理

response.setContentType(fileminitype);
response.setHeader("Location",filename);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
response.setHeader("Content-Disposition", "attachment; filename=" + filename); //filename应该是编码后的(utf-8)
response.setContentLength(filelength);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();
outputStream = null;
全部回答
  • 1楼网友:纵马山川剑自提
  • 2021-04-06 22:26
    public map fileupload_java(httpservletrequest request,string uploadpath) {      map param = new hashmap();      try {             // 参数或者文件名             string name = null;             // 参数的value             string value = null;             // 读取的流是否为文件的标志位             boolean fileflag = false;             // 要存储的文件。             file tmpfile = null;             // 上传的文件的名字             string fname = null;             fileoutputstream baos = null;             bufferedoutputstream bos = null;             int rtnpos = 0;             byte[] buffs = new byte[bufsize * 8];             // 取得contenttype             string contenttype = request.getcontenttype();             int index = contenttype.indexof("boundary=");             string boundary = "--" + contenttype.substring(index + 9);             string endboundary = boundary + "--";             // 从request对象中取得流。             servletinputstream sis = request.getinputstream();             // 读取1行             while ((rtnpos = sis.readline(buffs, 0, buffs.length)) != -1) {                 string strbuff = new string(buffs, 0, rtnpos);                 if (strbuff.startswith(boundary)) {                     if (name != null && name.trim().length() > 0) {                         if (fileflag) {                             bos.flush();                             baos.close();                             bos.close();                             baos = null;                             bos = null;                             param.put(name, tmpfile.getabsolutepath());                         } else {                             string paramvalue = param.get(name);                             paramvalue += ","+ value;                             param.put(name, paramvalue);                         }                     }                     name = new string();                     value = new string();                     fileflag = false;                     fname = new string();                     rtnpos = sis.readline(buffs, 0, buffs.length);                     if (rtnpos != -1) {                         strbuff = new string(buffs, 0, rtnpos);                         if (strbuff.tolowercase().startswith("content-disposition: form-data; ")) {                             int nindex = strbuff.tolowercase().indexof("name=\"");                             int nlastindex = strbuff.tolowercase().indexof("\"", nindex + 6);                             name = strbuff.substring(nindex + 6, nlastindex);                         }                         int findex = strbuff.tolowercase().indexof("filename=\"");                         if (findex != -1) {                             fileflag = true;                             int flastindex = strbuff.tolowercase().indexof("\"", findex + 10); //                            fname = strbuff.substring(findex + 10, flastindex);                             fname = new string(strbuff.substring(findex + 10, flastindex).getbytes(),"gbk");                             fname = filel.getfilenamewithoutseprater(fname);                             if (fname == null || fname.trim().length() == 0) {                                 fileflag = false;                                 sis.readline(buffs, 0, buffs.length);                                 sis.readline(buffs, 0, buffs.length);                                 sis.readline(buffs, 0, buffs.length);                                 continue;                             }else{                                 fname = filel.getfilenametime(fname);                                 sis.readline(buffs, 0, buffs.length);                                 sis.readline(buffs, 0, buffs.length);                             }                         }                     }                 } else if (strbuff.startswith(endboundary)) {                  if (name != null && name.trim().length() > 0) {                         if (fileflag) {                             bos.flush();                             baos.close();                             bos.close();                             baos = null;                             bos = null;                             param.put(name, tmpfile.getabsolutepath());                         } else {                             string paramvalue = param.get(name);                             paramvalue += ","+ value;                             param.put(name, paramvalue);                         }                     }                 } else {                     if (fileflag) {                         if (baos == null && bos == null) {                             tmpfile = new file(uploadpath + fname);                             baos = new fileoutputstream(tmpfile);                             bos = new bufferedoutputstream(baos);                         }                         bos.write(buffs, 0, rtnpos);                         baos.flush();                     } else {                         value = value + strbuff;                     }                 }             }         } catch (ioexception e) {             e.printstacktrace();         }         return param;     }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯