文件下载时出现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 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:

上传


用户名:

上传文件:




下载:


 
 
 

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

你可能感兴趣的:(javaweb--错误)