文件下载时出现D:\upload\uploadfile (拒绝访问。)

1.win7磁盘权限的问题,文件夹权限问题

解决方法:

链接:http://jingyan.baidu.com/article/f71d6037582b441ab641d195.html

2.是否是文件名没写,只写路径

下载:

public class DownLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;  
    public DownLoadServlet() {    
    }
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 filename=request.getParameter("filename");
String filepath=request.getParameter("filepath");
System.out.println(filename);
System.out.println(filepath);
response.setContentType("application/-msdownload");
// response.setHeader("Content-disposition","attachment;filename="+filename);
response.setHeader("Content-disposition","attachment;filename="+new String(filename.getBytes("GBK"),"ISO-8859-1"));

FileInputStream fis=new FileInputStream(filepath);
byte[] data =new byte[fis.available()];
fis.read(data);
fis.close();
OutputStream os=response.getOutputStream();
os.write(data);
os.close();
}
}



上传:

public class uploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    public uploadServlet() {
          }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
 
FileItemFactory factory=new DiskFileItemFactory(); 
ServletFileUpload sf=new ServletFileUpload(factory);
List<FileItem> flist=null;
         try {
flist=sf.parseRequest(request);
for(FileItem   f:   flist){

//判断是否是表单域
if(f.isFormField()){
  //System.out.println(f.getFieldName());

//判断表单域中name值是否为username
if(f.getFieldName().equals("username")){
String username=f.getString("utf-8");
//System.out.println("username:"+username);
}

if(f.getFieldName().equals("password")){
String password=f.getString();
//System.out.println("password:"+password);
}
  }else{
 // System.out.println(f.getName());
// String uri="c:\\"+f.getName();
// File file=new File(uri);
// f.write(file);
       String filename=f.getName();//得到上传的文件名
       String extraname=filename.substring(filename.lastIndexOf(".")+1);//截取文件名后 . 后面的后缀名  
                 String uri = this.getServletContext().getRealPath("file");
                 //System.out.println(uri);
                 if(extraname.equals("jpg") || extraname.equals("gif")){
                          File fi=new File(uri);

//如果文件不存在,创建文件
                          if(!fi.exists()){    
                        fi.mkdirs(); 
                          }
                           File file=new File(fi,f.getName());
                           f.write(file);
                         // f.write(fi);
                 }else{
                    System.out.println(f.getName()+"没有上传成功");
                 }

}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
}
}

jsp:

上传

<form action="UplodServlet" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username"><br>
上传文件:<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>


下载:

<form action="DownloadServlet1" method="post">
 <input type="hidden" name="filepath" value="D:\file\商品表.sql">
 <input type="hidden" name="filename" value="商品表.sql">
 <input type="submit" value="下载">
</form>

下载的jsp要是没有写    文件名 商品表.sql     也会显示拒绝访问

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值